console/integration-tests/scripts/pack.mjs
Kamil Kisiela dc3bc1ec87 Hello
2022-05-18 09:26:57 +02:00

103 lines
2.6 KiB
JavaScript

/**
* !! Node !!
*
* Gets all the packages from the manifest and packs them.
* As a result, we get a tarball for each package in the integration-tests/tarballs directory.
*
* Naming convention:
* @hive/tokens -> tokens.tgz
*/
import { exec } from 'child_process';
import path from 'path';
import fs from 'fs';
import fsExtra from 'fs-extra';
import glob from 'glob';
import rimraf from 'rimraf';
import { fileURLToPath } from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const cwd = path.resolve(__dirname, '../..');
const tarballDir = path.resolve(cwd, 'integration-tests/tarballs');
async function main() {
rimraf.sync(`${tarballDir}`, {});
fsExtra.mkdirSync(tarballDir, { recursive: true });
function isBackendPackage(manifestPath) {
return JSON.parse(
fs.readFileSync(manifestPath, 'utf-8')
).buildOptions?.tags.includes('backend');
}
function listBackendPackages() {
const manifestPathCollection = glob.sync(
'packages/services/*/package.json',
{
cwd,
absolute: true,
ignore: ['**/node_modules/**', '**/dist/**'],
}
);
return manifestPathCollection
.filter(isBackendPackage)
.map((filepath) => path.relative(cwd, path.dirname(filepath)));
}
async function pack(location) {
const { version, name } = JSON.parse(
await fsExtra.readFile(path.join(cwd, location, 'package.json'), 'utf-8')
);
const stdout = await new Promise((resolve, reject) => {
exec(
`npm pack ${path.join(cwd, location, 'dist')}`,
{
cwd,
encoding: 'utf8',
},
(err, stdout, stderr) => {
console.log(stderr);
if (err) {
reject(err);
} else {
resolve(stdout);
}
}
);
});
const lines = stdout.split('\n');
const org_filename = path.resolve(cwd, lines[lines.length - 2]);
let filename = org_filename
.replace(cwd, tarballDir)
.replace('hive-', '')
.replace(`-${version}`, '');
if (/-\d+\.\d+\.\d+\.tgz$/.test(filename)) {
throw new Error(`Build ${name} package first!`);
}
await fsExtra.rename(org_filename, filename);
return filename;
}
const locations = listBackendPackages();
await Promise.all(
locations.map(async (loc) => {
try {
const filename = await pack(loc);
console.log('[pack] Done', path.resolve(cwd, filename));
} catch (error) {
console.error(`[pack] Failed to pack ${loc}: ${error}`);
console.error('[pack] Maybe you forgot to build the packages first?');
process.exit(1);
}
})
);
}
await main();