Skip to content

Commit

Permalink
Add helper for copying files and dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
ashley-hebler committed Dec 11, 2020
1 parent 17b8299 commit fbbe36f
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 2 deletions.
5 changes: 3 additions & 2 deletions examples/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const { styles, icons, amp } = require('../lib');
const { AMP_MAP, CSS_MAP, MANIFEST_FILE, SVG_MAP } = require('./paths');
const { styles, icons, amp, copy } = require('../lib');
const { AMP_MAP, CSS_MAP, MANIFEST_FILE, SVG_MAP, COPY_MAP } = require('./paths');

async function build() {
await styles(CSS_MAP, MANIFEST_FILE);
await icons(SVG_MAP);
await amp(AMP_MAP);
await copy(COPY_MAP);
}

build().catch(err => {
Expand Down
12 changes: 12 additions & 0 deletions examples/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,23 @@ const SVG_MAP = [
},
];

const COPY_MAP = [
{
in: inputPath('bug.svg'),
out: outputPath('copied-file/bug.svg'),
},
{
in: inputPath('icons'),
out: outputPath('copied-dir/icons'),
},
];

const MANIFEST_FILE = outputPath('styles.json');

module.exports = {
AMP_MAP,
CSS_MAP,
SVG_MAP,
MANIFEST_FILE,
COPY_MAP,
};
2 changes: 2 additions & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const SCSS_SUCCESS = 'Compiled SCSS';
const SVG_SUCCESS = 'Built sprite';
const AMP_SUCCESS = 'Built AMP stylesheet';
const COPY_SUCCESS = 'Copied assets';
const AMP_BOILERPLATE = `<!doctype html>
<html amp lang="en">
<head>
Expand All @@ -19,6 +20,7 @@ const AMP_BOILERPLATE = `<!doctype html>
module.exports = {
AMP_BOILERPLATE,
AMP_SUCCESS,
COPY_SUCCESS,
SCSS_SUCCESS,
SVG_SUCCESS,
};
34 changes: 34 additions & 0 deletions lib/copy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// utility packages
const fs = require('fs-extra');
const ora = require('ora');

// internal
const { COPY_SUCCESS } = require('./constants');

const spinner = ora();

const copyDirs = async dirMap => {
try {
await fs.copy(dirMap.in, dirMap.out, { preserveTimestamps: true });
} catch (err) {
spinner.warn(
'Copying assets: Note that if input is a file, output cannot be a directory.'
);
throw err
}
return COPY_SUCCESS;
};

module.exports = async dirs => {
spinner.start('Copying assets');

return Promise.all(dirs.map(dirMap => copyDirs(dirMap)))
.then(resp => {
spinner.succeed();
return resp;
})
.catch(error => {
spinner.fail();
throw new Error(error.message);
});
};
2 changes: 2 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ const styles = require('./styles.js');
const icons = require('./icons.js');
const utils = require('./utils.js');
const amp = require('./amp.js');
const copy = require('./copy.js');

module.exports = {
styles,
icons,
utils,
amp,
copy
};
8 changes: 8 additions & 0 deletions tests/copy.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { copy } = require('../lib');
const { COPY_MAP } = require('../examples/paths');
const { COPY_SUCCESS } = require('../lib/constants');

it('Copies directories or files', async () => {
const data = await copy(COPY_MAP);
expect(data).toStrictEqual([COPY_SUCCESS, COPY_SUCCESS]);
});

0 comments on commit fbbe36f

Please sign in to comment.