Mirror: The highly customizable and versatile GraphQL client with which you add on features like normalized caching as you grow.
at main 2.2 kB view raw
1import * as path from 'path'; 2import * as fs from 'fs/promises'; 3import glob from 'glob'; 4 5import { workspaceRoot, workspaces, require } from './constants.mjs'; 6 7const getPackageManifest = cwd => require(path.resolve(cwd, 'package.json')); 8 9const updatePackageManifest = async (cwd, manifest) => { 10 const sortDependencies = dependencies => { 11 if (dependencies == null) return undefined; 12 return Object.keys(dependencies) 13 .sort() 14 .reduce((acc, key) => { 15 acc[key] = dependencies[key]; 16 return acc; 17 }, {}); 18 }; 19 20 try { 21 if (!!getPackageManifest(cwd)) { 22 manifest.dependencies = sortDependencies(manifest.dependencies); 23 manifest.devDependencies = sortDependencies(manifest.devDependencies); 24 await fs.writeFile( 25 path.resolve(cwd, 'package.json'), 26 JSON.stringify(manifest, null, 2) + '\n' 27 ); 28 } 29 } catch (_error) { 30 throw new Error('package.json does not exist in: ' + cwd); 31 } 32}; 33 34const getPackageArtifact = cwd => { 35 const pkg = getPackageManifest(cwd); 36 const name = 37 pkg.name[0] === '@' ? pkg.name.slice(1).replace(/\//g, '-') : pkg.name; 38 return `${name}-v${pkg.version}.tgz`; 39}; 40 41const listPackages = async () => { 42 let manifests = await Promise.all( 43 workspaces.map(dir => glob(`${dir}/package.json`)) 44 ); 45 46 manifests = manifests.reduce((acc, manifests) => { 47 acc.push(...manifests); 48 return acc; 49 }, []); 50 51 let packages = manifests 52 .filter(pkg => !require(path.join(workspaceRoot, pkg)).private) 53 .map(pkg => path.resolve(pkg, '../')); 54 55 if (process.env.NODE_TOTAL) { 56 const nodeTotal = parseInt(process.env.NODE_TOTAL, 10) || 1; 57 const nodeIndex = parseInt(process.env.NODE_INDEX, 10) % nodeTotal; 58 packages = packages.filter((_, i) => i % nodeTotal === nodeIndex); 59 console.log(`> Node ${nodeIndex + 1} of ${nodeTotal}.`); 60 } 61 62 return packages; 63}; 64 65const listArtifacts = async () => { 66 return (await listPackages()).map(cwd => { 67 const artifact = getPackageArtifact(cwd); 68 return path.resolve(cwd, artifact); 69 }); 70}; 71 72export { 73 getPackageManifest, 74 updatePackageManifest, 75 getPackageArtifact, 76 listPackages, 77 listArtifacts, 78};