Skip to content

Commit

Permalink
feat(cli): create output folder if it doesn't exist
Browse files Browse the repository at this point in the history
fix #9
  • Loading branch information
onderceylan committed Aug 15, 2019
1 parent 1fb1ecb commit 4eebf34
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Automatically generated files by app
static
temp

# Logs
logs
Expand Down
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
static/*
!static/.gitkeep
temp/*
.editorconfig
.eslintrc
.gitattributes
Expand Down
20 changes: 12 additions & 8 deletions cli.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const execa = require('execa');
const file = require('./helpers/file');

test('throws error when input is not provided', async () => {
try {
Expand All @@ -8,21 +9,24 @@ test('throws error when input is not provided', async () => {
}
});

test('throws error when wrong output is provided', async () => {
try {
await execa.sync('./cli.js', ['./static/logo.png', 'unknown_folder']);
} catch (e) {
expect(e.stderr).toContain(
'Make sure output folder unknown_folder exists and writable',
);
}
test('creates an output folder when output path does not exist', async () => {
jest.setTimeout(30000);
const tempFolderName = 'temp';
await execa.sync('./cli.js', [
'./static/logo.png',
tempFolderName,
'-s=false',
'--icon-only',
]);
expect(await file.pathExists(tempFolderName)).toBe(true);
});

test('creates images', async () => {
jest.setTimeout(30000);
let flags = `
--background="rgba(255, 255, 255, .5)"
--scrape=false
--icon-only
`;

flags = flags
Expand Down
13 changes: 13 additions & 0 deletions helpers/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ const pathExists = (filePath, mode) => {
});
};

const makeDir = folderPath => {
return new Promise((resolve, reject) => {
fs.mkdir(folderPath, { recursive: true }, err => {
if (err) {
return reject(err);
}

return resolve();
});
});
};

const readFile = (filePath, options) => {
return new Promise((resolve, reject) => {
fs.readFile(filePath, options, (err, data) => {
Expand Down Expand Up @@ -134,6 +146,7 @@ module.exports = {
getExtension,
readFile,
writeFile,
makeDir,
READ_ACCESS: fs.constants.R_OK,
WRITE_ACCESS: fs.constants.W_OK,
};
6 changes: 5 additions & 1 deletion puppets.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ const saveImages = async (imageList, source, output, options) => {
};

const generateImages = async (source, output, options) => {
const logger = preLogger(generateImages.name);
const splashScreenMetaData = await getSplashScreenMetaData(options);
const allImages = [
...(!options.iconOnly
Expand All @@ -250,7 +251,10 @@ const generateImages = async (source, output, options) => {
];

if (!(await file.pathExists(output, file.WRITE_ACCESS))) {
throw Error(`Make sure output folder ${output} exists and writable`);
logger.warn(
`Looks like folder ${output} doesn't exist. Created one for you`,
);
await file.makeDir(output);
}

// Increase MaxListeners and suppress MaxListenersExceededWarning
Expand Down

0 comments on commit 4eebf34

Please sign in to comment.