2021-05-03 17:15:13 +00:00
|
|
|
import { promises as fs, readFileSync } from 'fs';
|
2021-05-17 22:52:11 +00:00
|
|
|
import { posix } from 'path';
|
2021-05-03 17:15:13 +00:00
|
|
|
import arg from 'arg';
|
2021-12-13 20:59:08 +00:00
|
|
|
import { globby as glob } from 'globby';
|
2021-05-03 17:15:13 +00:00
|
|
|
import tar from 'tar';
|
|
|
|
|
2021-05-17 22:52:11 +00:00
|
|
|
const { resolve, dirname, sep, join } = posix;
|
|
|
|
|
2021-05-03 17:15:13 +00:00
|
|
|
/** @type {import('arg').Spec} */
|
|
|
|
const spec = {
|
2021-12-22 21:11:05 +00:00
|
|
|
'--tgz': Boolean,
|
2021-05-03 17:15:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default async function copy() {
|
2021-12-22 21:11:05 +00:00
|
|
|
let { _: patterns, ['--tgz']: isCompress } = arg(spec);
|
|
|
|
patterns = patterns.slice(1);
|
2021-05-03 17:15:13 +00:00
|
|
|
|
2021-12-22 21:11:05 +00:00
|
|
|
if (isCompress) {
|
|
|
|
const files = await glob(patterns, { gitignore: true });
|
|
|
|
const rootDir = resolveRootDir(files);
|
|
|
|
const destDir = rootDir.replace(/^[^/]+/, 'dist');
|
2021-05-03 17:15:13 +00:00
|
|
|
|
2021-12-22 21:11:05 +00:00
|
|
|
const templates = files.reduce((acc, curr) => {
|
|
|
|
const name = curr.replace(rootDir, '').slice(1).split(sep)[0];
|
|
|
|
if (acc[name]) {
|
|
|
|
acc[name].push(resolve(curr));
|
|
|
|
} else {
|
|
|
|
acc[name] = [resolve(curr)];
|
|
|
|
}
|
|
|
|
return acc;
|
|
|
|
}, {});
|
2021-05-03 17:15:13 +00:00
|
|
|
|
2021-12-22 21:11:05 +00:00
|
|
|
let meta = {};
|
|
|
|
return Promise.all(
|
|
|
|
Object.entries(templates).map(([template, files]) => {
|
|
|
|
const cwd = resolve(join(rootDir, template));
|
|
|
|
const dest = join(destDir, `${template}.tgz`);
|
|
|
|
const metafile = files.find((f) => f.endsWith('meta.json'));
|
|
|
|
if (metafile) {
|
|
|
|
files = files.filter((f) => f !== metafile);
|
|
|
|
meta[template] = JSON.parse(readFileSync(metafile).toString());
|
|
|
|
}
|
|
|
|
return fs.mkdir(dirname(dest), { recursive: true }).then(() =>
|
|
|
|
tar.create(
|
|
|
|
{
|
|
|
|
gzip: true,
|
|
|
|
portable: true,
|
|
|
|
file: dest,
|
|
|
|
cwd,
|
|
|
|
},
|
|
|
|
files.map((f) => f.replace(cwd, '').slice(1))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
})
|
|
|
|
).then(() => {
|
|
|
|
if (Object.keys(meta).length > 0) {
|
|
|
|
return fs.writeFile(resolve(destDir, 'meta.json'), JSON.stringify(meta, null, 2));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-05-03 17:15:13 +00:00
|
|
|
|
2021-12-22 21:11:05 +00:00
|
|
|
const files = await glob(patterns);
|
|
|
|
await Promise.all(
|
|
|
|
files.map((file) => {
|
|
|
|
const dest = resolve(file.replace(/^[^/]+/, 'dist'));
|
2022-04-02 20:15:41 +00:00
|
|
|
return fs
|
|
|
|
.mkdir(dirname(dest), { recursive: true })
|
|
|
|
.then(() => fs.copyFile(resolve(file), dest));
|
2021-12-22 21:11:05 +00:00
|
|
|
})
|
|
|
|
);
|
2021-05-03 17:15:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function resolveRootDir(files) {
|
2021-12-22 21:11:05 +00:00
|
|
|
return files
|
|
|
|
.reduce((acc, curr) => {
|
|
|
|
const currParts = curr.split(sep);
|
|
|
|
if (acc.length === 0) return currParts;
|
|
|
|
const result = [];
|
|
|
|
currParts.forEach((part, i) => {
|
|
|
|
if (acc[i] === part) result.push(part);
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}, [])
|
|
|
|
.join(sep);
|
2021-04-30 21:33:35 +00:00
|
|
|
}
|