Skip to content

Instantly share code, notes, and snippets.

@tqwewe
Last active July 22, 2020 10:48
Show Gist options
  • Save tqwewe/9160c16503cea44014cc31a33d2326d5 to your computer and use it in GitHub Desktop.
Save tqwewe/9160c16503cea44014cc31a33d2326d5 to your computer and use it in GitHub Desktop.
NodeJS Yarn Run Develop Generic
// This script searches for a package json in current directory, and works it's way up until one is found.
// If no package.json is found, the dev command returned is `develop`.
// If a package.json is found, it reads the scripts section and finds one that matches: 'dev', 'develop', 'start'
// The result returned is `yarn install && yarn run <dev_command>` (with dev_command being the found develop command.
// This can be fed into bash along with an alias defined to create an easy way to run the develop command in projects.
//
// Put this dev.js file in ~/scripts/dev.js
//
// Add this to your .zshrc file (or .bashrc)
// alias dev="node ~/scripts/dev.js | bash"
//
// Anytime you want to run a project, just run `dev` (it will automatically yarn install too).
//
// A quick installation command if you're lazy (written for zsh):
//
// > mkdir ~/scripts; curl https://gist.githubusercontent.com/Acidic9/9160c16503cea44014cc31a33d2326d5/raw/eb431aa5b7e8c9eb387171f9c57ff06deb478145/dev.js --output ~/scripts/dev.js && echo 'alias dev="node ~/scripts/dev.js | bash"' >> ~/.zshrc && source ~/.zshrc
//
const path = require("path");
const fs = require("fs");
const findUp = async (dir, searchFile) => {
const stuffHere = await new Promise((resolve, reject) =>
fs.readdir(dir, (err, files) => {
if (err) reject(err);
resolve(files);
})
);
const stuffHereDirs = await Promise.all(
stuffHere.map(
(thingHere) =>
new Promise((resolve, reject) => {
fs.stat(path.join(dir, thingHere), (err, stats) => {
if (err) resolve(false);
resolve(stats ? stats.isDirectory() : false);
});
})
)
);
const dirsHere = stuffHere.filter((thingHere, index) => stuffHereDirs[index]);
const filesHere = stuffHere.filter(
(thingHere, index) => !stuffHereDirs[index]
);
// Have we found the file?
const fileFound = filesHere.find((fileHere) => {
if (fileHere === searchFile) {
return true;
}
return false;
});
if (fileFound) return path.join(dir, fileFound);
// Aint here... lets go up a level for each dir here
if (dir === "/") {
throw new Error("file not found");
}
return await findUp(path.join(dir, ".."), searchFile);
};
const getDevCommand = async (fallback, whitelist) => {
let packageJson;
try {
const packageJsonPath = await findUp(process.cwd(), "package.json");
packageJson = require(packageJsonPath);
} catch (err) {
console.log('echo "[WARN] No package.json file found"');
process.exit(1);
}
if (!packageJson || !packageJson.scripts) {
return fallback;
}
return (
Object.keys(packageJson.scripts).find((cmd) =>
whitelist.some((c) => c === cmd)
) || fallback
);
};
const main = async () => {
const devCommand = await getDevCommand("develop", [
"dev",
"develop",
"start",
]);
console.log(`yarn install && yarn run ${devCommand}`);
};
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment