diff options
author | 2021-05-03 12:15:13 -0500 | |
---|---|---|
committer | 2021-05-03 12:15:13 -0500 | |
commit | ed631329e731d31e384dacc1ec399ba60b7c906b (patch) | |
tree | 0c998bb642c1feab773cb0e751dbb0ece1e59d64 /scripts/cmd/copy.js | |
parent | 467820996f71b0c78f2000294cb6f3c0a8f3aca4 (diff) | |
download | astro-ed631329e731d31e384dacc1ec399ba60b7c906b.tar.gz astro-ed631329e731d31e384dacc1ec399ba60b7c906b.tar.zst astro-ed631329e731d31e384dacc1ec399ba60b7c906b.zip |
`create-astro` UI (#164)
* refactor: improve create-astro layout, build script
* feat(create-astro): v0.1.0
* docs(create-astro): add README
* feat(create-astro): add meta files to starter templates
Diffstat (limited to 'scripts/cmd/copy.js')
-rw-r--r-- | scripts/cmd/copy.js | 83 |
1 files changed, 73 insertions, 10 deletions
diff --git a/scripts/cmd/copy.js b/scripts/cmd/copy.js index 0fffae1c3..6b7e9deee 100644 --- a/scripts/cmd/copy.js +++ b/scripts/cmd/copy.js @@ -1,11 +1,74 @@ -import { promises as fs } from 'fs'; -import { resolve, dirname } from 'path'; -import glob from 'tiny-glob'; - -export default async function copy(pattern, ...args) { - const files = await glob(pattern, { filesOnly: true }); - await Promise.all(files.map(file => { - const dest = resolve(file.replace(/^[^/]+/, 'dist')); - return fs.mkdir(dirname(dest), { recursive: true }).then(() => fs.copyFile(resolve(file), dest)) - })); +import { promises as fs, readFileSync } from 'fs'; +import { resolve, dirname, sep, join } from 'path'; +import arg from 'arg'; +import glob from 'globby'; +import tar from 'tar'; + +/** @type {import('arg').Spec} */ +const spec = { + '--tgz': Boolean, +}; + +export default async function copy() { + let { _: patterns, ['--tgz']: isCompress } = arg(spec); + patterns = patterns.slice(1); + + if (isCompress) { + const files = await glob(patterns, { gitignore: true }); + const rootDir = resolveRootDir(files); + const destDir = rootDir.replace(/^[^/]+/, 'dist'); + + 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; + }, {}); + + 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)); + } + }); + } + + const files = await glob(patterns); + await Promise.all(files.map(file => { + const dest = resolve(file.replace(/^[^/]+/, 'dist')); + return fs.mkdir(dirname(dest), { recursive: true }).then(() => fs.copyFile(resolve(file), dest)) + })); +} + +function resolveRootDir(files) { + 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); } |