Skip to content

Commit

Permalink
Add -f option
Browse files Browse the repository at this point in the history
  • Loading branch information
ta-Hirose committed Aug 16, 2022
1 parent af1f1f5 commit 24896d6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
4 changes: 4 additions & 0 deletions dim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ await new Command()
"Specify the header. Can specify multiple times.",
{ collect: true },
)
.option(
"-f, --file <file:string>",
"Specify the dim.json file when installing data.",
)
.option(
"-F, --force",
"Forced install. Overwrite already exist data file.",
Expand Down
6 changes: 3 additions & 3 deletions libs/accessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import {

export class DimFileAccessor {
private dimJSON: DimJSON | undefined;
constructor() {
if (existsSync(DEFAULT_DIM_FILE_PATH)) {
this.dimJSON = JSON.parse(Deno.readTextFileSync(DEFAULT_DIM_FILE_PATH));
constructor(path = DEFAULT_DIM_FILE_PATH) {
if (existsSync(path)) {
this.dimJSON = JSON.parse(Deno.readTextFileSync(path));
} else {
console.log("Not found a dim.json. You should run a 'dim init'. ");
Deno.exit(0);
Expand Down
36 changes: 26 additions & 10 deletions libs/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ const installFromURL = async (
return result;
};

const installFromDimFile = async (isUpdate = false) => {
let contents = new DimFileAccessor().getContents();
const installFromDimFile = async (path: string, isUpdate = false) => {
let contents = new DimFileAccessor(path).getContents();

if (contents.length == 0) {
console.log("No contents.\nYou should run a 'dim install <data url>'. ");
return;
Expand Down Expand Up @@ -168,10 +169,17 @@ export class InstallAction {
postProcesses?: string[];
name?: string;
headers?: string[];
file?: string;
force?: boolean;
},
url: string | undefined,
) {
if (url && options.file) {
console.log(
Colors.red("Cannot use -f option and URL at the same time."),
);
Deno.exit(1);
}
await createDataFilesDir();
if (!existsSync(DEFAULT_DIM_LOCK_FILE_PATH)) {
await initDimLockFile();
Expand Down Expand Up @@ -239,7 +247,10 @@ export class InstallAction {
Colors.green(`Installed to ${fullPath}`),
);
} else {
const lockContentList = await installFromDimFile(options.force).catch(
const lockContentList = await installFromDimFile(
options.file || DEFAULT_DIM_FILE_PATH,
options.force,
).catch(
(error) => {
console.error(
Colors.red("Failed to install."),
Expand Down Expand Up @@ -451,13 +462,18 @@ export class UpdateAction {
Colors.yellow(fullPath),
);
} else {
const lockContentList = await installFromDimFile(true).catch((error) => {
console.error(
Colors.red("Failed to update."),
Colors.red(error.message),
);
Deno.exit(1);
});
const lockContentList = await installFromDimFile(
DEFAULT_DIM_FILE_PATH,
true,
).catch(
(error) => {
console.error(
Colors.red("Failed to update."),
Colors.red(error.message),
);
Deno.exit(1);
},
);
if (lockContentList !== undefined) {
await new DimLockFileAccessor().addContents(lockContentList);
}
Expand Down

0 comments on commit 24896d6

Please sign in to comment.