Skip to content

Instantly share code, notes, and snippets.

@samrocksc
Last active April 4, 2020 10:16
Show Gist options
  • Save samrocksc/8afdeb5f40dcc982f06e585eca19e245 to your computer and use it in GitHub Desktop.
Save samrocksc/8afdeb5f40dcc982f06e585eca19e245 to your computer and use it in GitHub Desktop.
holy fucking stupid
const { readFileSync, readdirSync } = require('fs');
const fsx = require('fs-extra');
const { promisify } = require('util');
const lernaConfig = require('./lerna.json');
/**
* grabs the lerna config, and then does a deep search of all packages and hard copies
* the node_modules that need to be
*/
const main = async () => {
// striping all the directory commands
const packageSubDir = lernaConfig.packages.map((package) =>
package.replace('/*', '').replace('//', ''),
);
// grabs packages directory array and flattens it
const packagesDir = packageSubDir
.map((location) =>
readdirSync(`${location}`).map(
(packageName) => `${location}/${packageName}`,
),
)
.flat();
// Grabs each of the lerna package's package.json and returns it with its location
const packageJsonMap = packagesDir.map((packageLocation) => {
const stringedJson = readFileSync(
`${__dirname}/${packageLocation}/package.json`,
).toString();
return {
location: packageLocation,
content: JSON.parse(stringedJson),
};
});
// gets the package names and lists and their file location
const packageNameList = packageJsonMap.map((packageJson) => ({
name: packageJson.content.name,
location: packageJson.location,
}));
// checks for dependencies that need to be hard copied and returns what and where to copy to
const packagesToUpdate = packageJsonMap
.map((packageJson) => {
// checks the deps of each package if any local lerna packages need to be pushed to it.
const deps = packageNameList.map((packageName) => {
if (
Object.keys(packageJson.content.dependencies).includes(
packageName.name,
)
) {
return {
copy: `${__dirname}/${packageName.location}`,
to: `${__dirname}/${packageJson.location}/node_modules/${packageName.name}`,
};
}
});
return deps; // returns friendly copy/to format
})
.flat() // flattens the array
.filter((dep) => dep !== undefined); // removes all undefined
const fileCopyArray = packagesToUpdate.map(async (packageToUpdate) => {
const { copy, to } = packageToUpdate;
try {
await fsx.remove(to); // removes the symlink
await fsx.copy(copy, to); // copies code into node_modules
} catch (e) {
throw e; // pray this doesn't happen
}
});
Promise.all(fileCopyArray);
};
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment