Skip to content

Instantly share code, notes, and snippets.

@joshmeads
Created July 23, 2020 19:21
Show Gist options
  • Save joshmeads/8740641292d6d336ea3b791a18282c7b to your computer and use it in GitHub Desktop.
Save joshmeads/8740641292d6d336ea3b791a18282c7b to your computer and use it in GitHub Desktop.
const fs = require('fs');
const yargs = require('yargs');
const { Argv } = yargs;
const { promisify } = require('util');
const { resolve } = require('path');
const exists = promisify(fs.exists);
const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);
const argv = yargs.options({
appFolder: {
demandOption: true,
description: 'app name',
type: 'string',
},
appVersion: {
default: '1.0.0',
description: 'version number',
type: 'string',
},
rootPath: {
default: resolve(__dirname, '..'),
description: 'project root path',
type: 'string',
},
docker: {
default: false,
type: 'boolean',
description: 'Running in docker?',
},
}).argv;
const root = argv.rootPath;
const distRoot = resolve(root, 'dist');
interface PackageJson {
dependencies: Record<string, string>;
}
interface ProjectDependency {
type: 'static' | 'implicit';
target: string;
}
interface NxDeps {
projectGraph: {
dependencies: Record<string, Array<ProjectDependency>>;
};
}
const generatePackageJson = (
name: string,
dependencies: Record<string, string>,
version: string
): string =>
JSON.stringify(
{
name,
version,
dependencies,
},
null,
2
);
const getDependencies = (
rootPackage: PackageJson,
deps: NxDeps,
pack: string
): Record<string, string> => {
const dependencies = deps.projectGraph.dependencies[pack];
const directDependencies = dependencies.reduce<Record<string, string>>(
(depsAcc: Record<string, string>, dep, index) => {
const depName = dep.target.split(':').pop();
const version = rootPackage.dependencies[depName];
if (dep.type === 'static') {
if (version) {
depsAcc[depName] = version;
} else {
return {
...depsAcc,
...getDependencies(rootPackage, deps, depName),
};
}
}
return depsAcc;
},
{}
);
return directDependencies;
};
const run = async (appFolder: any, version: any, docker: boolean) => {
const app = appFolder.split('/').join('-');
const appDir = docker ? [distRoot] : [distRoot, 'apps', appFolder];
const rootPackageBuffer = await readFile(
resolve(root, docker ? 'package-base.json' : 'package.json')
);
const rootPackage = JSON.parse(rootPackageBuffer.toString('utf-8'));
const depsBuffer = await readFile(
resolve(docker ? root : distRoot, 'nxdeps.json')
);
const deps = JSON.parse(depsBuffer.toString('utf-8')) as NxDeps;
console.log(`Checking ${appFolder}`);
const doesExist = await exists(resolve(...appDir));
if (doesExist && Reflect.has(deps.projectGraph.dependencies, app)) {
console.log(`Generating package.json at ${appDir.join('/')}/package.json`);
const dependencies = getDependencies(rootPackage, deps, app);
const appPackageJson = generatePackageJson(app, dependencies, version);
await writeFile(resolve(...appDir, 'package.json'), appPackageJson);
console.log('Done');
} else {
console.log('App not found');
}
};
if (argv.appFolder) {
run(argv.appFolder, argv.appVersion, argv.docker).then(() => process.exit(0));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment