Skip to content

Commit

Permalink
feat(cli): create output folder if it's not present. (itgalaxy#280)
Browse files Browse the repository at this point in the history
Creates the destination/output folder if it is not preset and the
`--dest-create` flag is provided in the command.

I decided to make it a command instead of just creating the
folder just incase the provided destination path was a mistake or
a typo and you don't want your fonts to be exported there.
  • Loading branch information
Ghustavh97 committed Nov 11, 2020
1 parent 4b1675f commit 2567959
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 28 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,10 @@ 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
4 changes: 4 additions & 0 deletions src/__mocks__/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ cli.showHelp = function () {
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
58 changes: 58 additions & 0 deletions src/__tests__/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,62 @@ describe("cli", () => {
expect(output.code).toBe(0);
expect(output.stderr).toBe("");
});

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) => {
/* eslint-disable-next-line no-unneeded-ternary */
const destinationWasCreated = accessError ? false : true;

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) => {
/* eslint-disable-next-line no-unneeded-ternary */
const destinationWasCreated = accessError ? false : true;

expect(destinationWasCreated).toBe(false);
fs.readdir(
nonExistentDestination,
{ encoding: "utf-8" },
(readdirError) => {
expect(() => {
throw readdirError;
}).toThrow(
new Error(
`ENOENT: no such file or directory, scandir '${nonExistentDestination}'`
)
);

done();
}
);
});
});
});
85 changes: 57 additions & 28 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ const cli = 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 @@ -161,6 +165,11 @@ const cli = meow(
default: process.cwd(),
type: "string",
},
"dest-create": {
alias: "m",
default: false,
type: "boolean",
},
"dest-template": {
alias: "s",
type: "string",
Expand Down Expand Up @@ -269,6 +278,10 @@ if (cli.flags.dest) {
optionsBase.dest = cli.flags.dest;
}

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

if (cli.flags.template) {
optionsBase.template = cli.flags.template;
}
Expand Down Expand Up @@ -389,7 +402,7 @@ Promise.resolve()
});
})
.then((result) => {
const { fontName, dest } = result.config;
const { fontName, dest, destCreate } = result.config;

let destTemplate = null;

Expand All @@ -415,35 +428,51 @@ Promise.resolve()
delete result.hash;
}

return Promise.resolve()
.then(() =>
Promise.all(
Object.keys(result).map((type) => {
if (
type === "config" ||
type === "usedBuildInTemplate" ||
type === "glyphsData"
) {
return null;
}

const content = result[type];

let file;

if (type !== "template") {
file = path.resolve(path.join(dest, `${fontName}.${type}`));
} else {
file = path.resolve(destTemplate);
}

return fs.writeFile(file, content, () => {
Function.prototype();
return (
Promise.resolve()
.then(
() =>
new Promise((resolve, reject) => {
fs.access(dest, fs.constants.F_OK, (err) => reject(err));
})
)
/* eslint-disable-next-line consistent-return */
.catch((error) => {
if (error && destCreate) {
return new Promise((resolve) => {
fs.mkdir(dest, { recursive: true }, () => resolve());
});
})
}
})
.finally(() =>
Promise.all(
Object.keys(result).map((type) => {
if (
type === "config" ||
type === "usedBuildInTemplate" ||
type === "glyphsData"
) {
return null;
}

const content = result[type];

let file;

if (type !== "template") {
file = path.resolve(path.join(dest, `${fontName}.${type}`));
} else {
file = path.resolve(destTemplate);
}

return fs.writeFile(file, content, () => {
Function.prototype();
});
})
)
)
)
.then(() => Promise.resolve(result));
.then(() => Promise.resolve(result))
);
})
.catch((error) => {
// eslint-disable-next-line no-console
Expand Down

0 comments on commit 2567959

Please sign in to comment.