Skip to content

Commit

Permalink
feat(cli): create output folder if it's not present. (#280)
Browse files Browse the repository at this point in the history
Closes #280 #307

Co-Authored-By: Ghustavh <Ghustavh97@gmail.com>
  • Loading branch information
Jimmy Andrade and Ghustavh97 committed May 12, 2021
1 parent d623b06 commit d66a0d4
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 23 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,9 @@ If you're using cross-env:

Destination for generated fonts.

-m, --dest-create
Create destination directory if it does not exist.

-t, --template

Type of template (\`css\`, \`scss\`, \`styl\`) or path to custom template.
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"dist",
"dist/src/index.d.ts",
"templates",
"!**/__tests__",
"!**/__mocks__",
"README.md"
],
"main": "dist/index.js",
Expand Down
43 changes: 43 additions & 0 deletions src/cli/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,4 +264,47 @@ describe("cli", () => {

});


it("should create dest directory if it does not exist and --dest-create flag is provided", async (done) => {
const nonExistentDestination = `${destination}/that/does/not/exist`;
const output = await execCLI(`${source} -d ${nonExistentDestination} --dest-create`);

fs.access(nonExistentDestination, fs.constants.F_OK, (accessError) => {

const destinationWasCreated = !accessError;

expect(destinationWasCreated).toBe(true);
fs.readdir(nonExistentDestination, { encoding: "utf-8" }, (readdirError, files) => {
if (readdirError) {
done(readdirError);

return;
}

output.files = files.filter((file) => file !== "that");

expect(output.files).toEqual(files);
done();
});
});
});

it("should not create dest directory if it does not exist", async (done) => {
const nonExistentDestination = `${destination}/that/does/not/exist`;

await execCLI(`${source} -d ${nonExistentDestination}`);

fs.access(nonExistentDestination, fs.constants.F_OK, (accessError) => {

const destinationWasCreated = !accessError;

expect(destinationWasCreated).toBe(false);
fs.readdir(nonExistentDestination, { encoding: "utf-8" }, (readdirError) => {
expect(readdirError.message).toContain("ENOENT: no such file or directory, scandir");

done();
});
});
});

});
40 changes: 17 additions & 23 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ if (cli.flags.formats) {
}

if (cli.flags.dest) {

optionsBase.dest = cli.flags.dest;
}

if (cli.flags.destCreate) {
optionsBase.destCreate = cli.flags.destCreate;
}

if (cli.flags.template) {
Expand Down Expand Up @@ -216,7 +218,7 @@ Promise.resolve().
}).
then((result: Result) => {

const {fontName, dest} = result.config;
const {fontName, dest, destCreate} = result.config;

let destTemplate = null;

Expand Down Expand Up @@ -245,40 +247,32 @@ Promise.resolve().
}

return Promise.resolve().
then(() => Promise.all(Object.keys(result).map((type) => {

if (
type === "config" ||
type === "usedBuildInTemplate" ||
type === "glyphsData"
) {

then(() => new Promise((resolve, reject) => {
fs.access(dest, fs.constants.F_OK, (err) => reject(err));
})).
catch((error) => {
if (error && destCreate) {
return new Promise((resolve) => {
fs.mkdir(dest, { recursive: true }, () => resolve(destCreate));
});
}
return error;
}).
finally(() => Promise.all(Object.keys(result).map((type) => {
if (type === "config" || type === "usedBuildInTemplate" || type === "glyphsData") {
return null;

}

const content = result[type];

// eslint-disable-next-line init-declarations
let file;

if (type === "template") {

file = path.resolve(destTemplate);


} else {

file = path.resolve(path.join(dest, `${fontName}.${type}`));

}

return fs.writeFile(file, content, () => {

Function.prototype();

});

}))).
then(() => Promise.resolve(result));

Expand Down
4 changes: 4 additions & 0 deletions src/cli/meow/__mocks__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ meowMock.showHelp = () => `
Destination for generated fonts.
-m, --dest-create
Create destination directory if it does not exist.
-t, --template
Type of template ('css', 'scss', 'styl') or path to custom template.
Expand Down
9 changes: 9 additions & 0 deletions src/cli/meow/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ const meowCLI = meow(`
Destination for generated fonts.
-m, --dest-create
Create destination directory if it does not exist.
-t, --template
Type of template ('css', 'scss', 'styl') or path to custom template.
Expand Down Expand Up @@ -154,6 +158,11 @@ const meowCLI = meow(`
default: process.cwd(),
type: "string",
},
destCreate: {
alias: "m",
default: false,
type: "boolean",
},
destTemplate: {
alias: "s",
type: "string",
Expand Down
1 change: 1 addition & 0 deletions src/types/OptionsBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {Formats} from "./Format";
export type OptionsBase = {
configFile?: string;
dest?: string;
destCreate?: boolean;
fontName?: string | unknown;
formats?: Formats;
template?: "css" | string;
Expand Down

0 comments on commit d66a0d4

Please sign in to comment.