Created
September 16, 2021 02:05
-
-
Save Maxim-Mazurok/76ac26fc04d8fc267bbbca9101986bd1 to your computer and use it in GitHub Desktop.
Running npm start for all projects in folder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { SH } from "./sh"; | |
import { join } from "path"; | |
import { readFileSync } from "fs"; | |
import { platform, EOL } from "os"; | |
const rootPath = join(__dirname, "vendors"); | |
const sh = new SH(rootPath); | |
const os = platform(); | |
const getDirs = async (rootPath: string): Promise<string[]> => { | |
switch (os) { | |
case "win32": | |
return (await sh.trySh(`dir ${rootPath}`)).output[0] | |
.trim() | |
.split(EOL) | |
.filter((x) => x.includes(`<DIR>`) && !x.includes(".")) | |
.map((x) => join(rootPath, x.split(" ").slice(-1)[0])); | |
case "linux": | |
return (await sh.trySh(`ls -d -A1 ${rootPath}/*/`)).output[0] | |
.trim() | |
.split(EOL); | |
default: | |
throw "not implemented"; | |
} | |
}; | |
const setNodeVersionCommands = (node: string): string[] => { | |
switch (os) { | |
case "win32": | |
return [`nvs add ${node}`, `nvs use ${node}`].map( | |
(x) => `${x} >nul 2>&1` | |
); | |
case "linux": | |
return [ | |
`source /home/maxim/.nvm/nvm.sh`, | |
`nvm i ${node} >/dev/null 2>&1`, | |
]; | |
default: | |
throw "not implemented"; | |
} | |
}; | |
(async () => { | |
// TODO: close chrome before running it (use `powershell -command "Get-Process chrome | ForEach-Object { $_.CloseMainWindow() | Out-Null}"` for cmd) | |
const dirs = await getDirs(rootPath); | |
for (const dirPath of dirs) { | |
const { | |
engines: { node, npm }, | |
} = JSON.parse(readFileSync(join(dirPath, "package.json"), "utf-8")); | |
const commands = [ | |
...setNodeVersionCommands(node), | |
`npm --silent i -g npm@${npm}`, | |
`npm --silent i`, | |
`npm start --silent`, | |
]; | |
const result = await sh.trySh(commands.join(" && "), dirPath); | |
const standardOutput = result.stdout.trim(); | |
if (standardOutput) { | |
console.log(result.stdout.trim()); | |
} | |
const errorOutput = result.stderr.trim(); | |
if (errorOutput) { | |
console.error(`Error:`, errorOutput); | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment