|
| 1 | +/** |
| 2 | + * dist/** --> {tmpDir}/{pkg.name}-{pkg.version}/** --> {tmpDir}/{pkg.name}-{pkg.version}.tgz |
| 3 | + */ |
| 4 | +import { ensureDir, readFile, remove, writeFile } from 'fs-extra'; |
| 5 | +import { dest, parallel, series, src } from 'gulp'; |
| 6 | +import tar from 'gulp-tar'; |
| 7 | +import path from 'path'; |
| 8 | + |
| 9 | +import { pkg, rootDir, tmpDir } from '../config'; |
| 10 | + |
| 11 | + |
| 12 | +// target destination for the tarball |
| 13 | +const FILEPATH = `${tmpDir}/${pkg.name}-${pkg.version}.tgz`; |
| 14 | +// where to build tarball contents |
| 15 | +const TMP_DIR = `${tmpDir}/package`; |
| 16 | +// name of the tarball |
| 17 | +const FILENAME = path.basename(FILEPATH); // "{NAME}-{VERSION}.tgz" |
| 18 | +// target directory |
| 19 | +const DEST_DIR = path.dirname(FILEPATH); // "{tmpDir}" |
| 20 | +/* |
| 21 | + * Collect globs for bundled dependency assets |
| 22 | + * (as configured via "bundledDependencies" in package.json) |
| 23 | + */ |
| 24 | +const BUNDLED = pkg.bundledDependencies.map(dep => `node_modules/${dep}/**/*`); |
| 25 | +/* |
| 26 | + * Define globs for all assets to be included in the package. |
| 27 | + * (relative to the root of the project) |
| 28 | + */ |
| 29 | +const GLOBS = [ |
| 30 | + 'LICENSE', |
| 31 | + 'README.md', |
| 32 | + // extra files |
| 33 | + 'dist/**/*', // TODO: use config vars to compose this glob |
| 34 | + ...BUNDLED, |
| 35 | +]; |
| 36 | + |
| 37 | + |
| 38 | +async function writePackageJSON () { |
| 39 | + let raw = await readFile(`${rootDir}/package.json`); |
| 40 | + let data = JSON.parse(raw); |
| 41 | + |
| 42 | + data.files = '*'; // include everything in {TMP_DIR} in the tarball |
| 43 | + |
| 44 | + // remove unnecessary keys |
| 45 | + delete data.scripts; |
| 46 | + delete data.nyc; |
| 47 | + |
| 48 | + await ensureDir(TMP_DIR); |
| 49 | + await writeFile(`${TMP_DIR}/package.json`, JSON.stringify(data, null, 2)); |
| 50 | +} |
| 51 | + |
| 52 | +function copyGlobFiles () { |
| 53 | + return src(GLOBS, { base: '.' }) |
| 54 | + .pipe(dest(TMP_DIR)); |
| 55 | +} |
| 56 | + |
| 57 | + |
| 58 | +async function _prepare () { |
| 59 | + await remove(TMP_DIR); |
| 60 | + await remove(FILEPATH); |
| 61 | +} |
| 62 | + |
| 63 | + |
| 64 | +/* |
| 65 | + * Package asset bundles into tarball. |
| 66 | + * |
| 67 | + * This is similar to how NPM works via `npm pack`, |
| 68 | + * but it allows us to have more control of the |
| 69 | + * directory structure within the tarball. |
| 70 | + * |
| 71 | + * NOTE: bundles MUST be generated beforehand |
| 72 | + */ |
| 73 | +function createArchive () { |
| 74 | + return src(`${TMP_DIR}/**/*`) |
| 75 | + .pipe(tar(FILENAME, { gzip: true })) // archive... |
| 76 | + .pipe(dest(DEST_DIR)); // write tarball to destination |
| 77 | +} |
| 78 | + |
| 79 | +const collectFiles = parallel( |
| 80 | + copyGlobFiles, |
| 81 | + writePackageJSON, |
| 82 | +); |
| 83 | + |
| 84 | +const archiveHelixUI = series( |
| 85 | + _prepare, |
| 86 | + collectFiles, |
| 87 | + createArchive, |
| 88 | +); |
| 89 | + |
| 90 | +export { |
| 91 | + FILEPATH, |
| 92 | + archiveHelixUI, |
| 93 | +}; |
0 commit comments