Skip to content

Commit

Permalink
fix(file): fixed an issue while creating a non-existent output dir
Browse files Browse the repository at this point in the history
When the output folder is non-existent, CLI creates a new folder for users. If a user provides a
non-existent directory tree, it's not able to resolve and create the tree for the user.

fix #77
  • Loading branch information
onderceylan committed Dec 17, 2019
1 parent a623757 commit 9a85eec
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
17 changes: 15 additions & 2 deletions src/helpers/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,22 @@ const getHtmlShell = (
)}`;
};

const pathExists = (
const isPathAccessible = (
filePath: string,
mode?: number | undefined,
): Promise<boolean> => promisify(fs.access)(filePath, mode).then(() => true);

// TODO: switch to fs.mkdir('', { recursive: true }) when node engine > 10.12.0 targeted
const makeDirRecursiveSync = (dirPath: string): string => {
return dirPath.split(path.sep).reduce((prevPath, folder) => {
const currentPath = path.join(prevPath, folder, path.sep);
if (!fs.existsSync(currentPath)) {
fs.mkdirSync(currentPath);
}
return currentPath;
}, '');
};

export default {
convertBackslashPathToSlashPath,
getRelativeImagePath,
Expand All @@ -133,7 +144,7 @@ export default {
getShellHtmlFilePath,
getImageSavePath,
getFileUrlOfPath,
pathExists,
isPathAccessible,
getRelativeFilePath,
getAppDir,
getExtension,
Expand All @@ -142,6 +153,8 @@ export default {
readFileSync: fs.readFileSync,
writeFile: promisify(fs.writeFile),
makeDir: promisify(fs.mkdir),
exists: promisify(fs.exists),
makeDirRecursiveSync,
READ_ACCESS: fs.constants.R_OK,
WRITE_ACCESS: fs.constants.W_OK,
};
4 changes: 2 additions & 2 deletions src/helpers/meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ const addIconsToManifest = async (
manifestContent: ManifestJsonIcon[],
manifestJsonFilePath: string,
): Promise<void> => {
if (!(await file.pathExists(manifestJsonFilePath, file.WRITE_ACCESS))) {
if (!(await file.isPathAccessible(manifestJsonFilePath, file.WRITE_ACCESS))) {
throw Error(`Cannot write to manifest json file ${manifestJsonFilePath}`);
}

Expand Down Expand Up @@ -197,7 +197,7 @@ const addMetaTagsToIndexPage = async (
htmlMeta: HTMLMeta,
indexHtmlFilePath: string,
): Promise<void> => {
if (!(await file.pathExists(indexHtmlFilePath, file.WRITE_ACCESS))) {
if (!(await file.isPathAccessible(indexHtmlFilePath, file.WRITE_ACCESS))) {
throw Error(`Cannot write to index html file ${indexHtmlFilePath}`);
}

Expand Down
9 changes: 7 additions & 2 deletions src/helpers/puppets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,16 @@ const generateImages = async (
...(!options.splashOnly ? images.getIconImages(options) : []),
];

if (!(await file.pathExists(output, file.WRITE_ACCESS))) {
if (
!(
(await file.exists(output)) &&
(await file.isPathAccessible(output, file.WRITE_ACCESS))
)
) {
file.makeDirRecursiveSync(output);
logger.warn(
`Looks like folder ${output} doesn't exist. Created one for you`,
);
await file.makeDir(output);
}

const savedImages = await saveImages(
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const getShellHtml = async (
return useShell(true);
}

if (!(await file.pathExists(source, file.READ_ACCESS))) {
if (!(await file.isPathAccessible(source, file.READ_ACCESS))) {
throw Error(`Cannot find path ${source}. Please check if file exists`);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ describe('generates meta', () => {
const readManifest = (): Promise<{ icons: ManifestJsonIcon[] }> =>
file
.readFile('./temp/manifest.json')
.then(resp => JSON.parse(resp as string));
.then(resp => JSON.parse((resp as unknown) as string));

test('creating icons array', async () => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down

0 comments on commit 9a85eec

Please sign in to comment.