From b691b6f8085c3fb4d384364a55bcfeb12ae10305 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96nder=20Ceylan?= Date: Tue, 17 Dec 2019 13:50:22 +0100 Subject: [PATCH] fix(file): fixed an issue while creating a non-existent output dir 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 --- src/helpers/file.ts | 17 +++++++++++++++-- src/helpers/meta.ts | 4 ++-- src/helpers/puppets.ts | 9 +++++++-- src/helpers/url.ts | 2 +- src/main.test.ts | 2 +- 5 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/helpers/file.ts b/src/helpers/file.ts index 926aa465..dc5c12d3 100644 --- a/src/helpers/file.ts +++ b/src/helpers/file.ts @@ -118,11 +118,22 @@ const getHtmlShell = ( )}`; }; -const pathExists = ( +const isPathAccessible = ( filePath: string, mode?: number | undefined, ): Promise => 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, @@ -133,7 +144,7 @@ export default { getShellHtmlFilePath, getImageSavePath, getFileUrlOfPath, - pathExists, + isPathAccessible, getRelativeFilePath, getAppDir, getExtension, @@ -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, }; diff --git a/src/helpers/meta.ts b/src/helpers/meta.ts index 926b5bcb..60b96a1b 100644 --- a/src/helpers/meta.ts +++ b/src/helpers/meta.ts @@ -150,7 +150,7 @@ const addIconsToManifest = async ( manifestContent: ManifestJsonIcon[], manifestJsonFilePath: string, ): Promise => { - 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}`); } @@ -197,7 +197,7 @@ const addMetaTagsToIndexPage = async ( htmlMeta: HTMLMeta, indexHtmlFilePath: string, ): Promise => { - 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}`); } diff --git a/src/helpers/puppets.ts b/src/helpers/puppets.ts index f28cd93b..4dbe4f80 100644 --- a/src/helpers/puppets.ts +++ b/src/helpers/puppets.ts @@ -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( diff --git a/src/helpers/url.ts b/src/helpers/url.ts index a8e03ae6..75e0e6d3 100644 --- a/src/helpers/url.ts +++ b/src/helpers/url.ts @@ -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`); } diff --git a/src/main.test.ts b/src/main.test.ts index 503550d9..6940ed7b 100644 --- a/src/main.test.ts +++ b/src/main.test.ts @@ -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