Comparing version 0.1.9 to 0.2.0
#!/usr/bin/env node | ||
export declare const findPackageJson: () => Promise<{ | ||
rootPackageJson: string | undefined; | ||
packageJson: string | undefined; | ||
rootPackageJsonContent: any; | ||
packageJsonContent: any; | ||
isRootPackageJson: boolean; | ||
}>; | ||
export declare const getPackageJsonScripts: (path: string | undefined) => Promise<{ | ||
[key: string]: string; | ||
} | undefined>; | ||
export declare const main: () => Promise<void>; |
@@ -11,4 +11,4 @@ #!/usr/bin/env node | ||
}; | ||
import { packageDirectory } from "pkg-dir"; | ||
import { rootPkgJSON } from "root-pkg-json"; | ||
import { packageDirectory } from "pkg-dir"; | ||
import fs from "fs/promises"; | ||
@@ -18,50 +18,20 @@ import { spawn } from "child_process"; | ||
import { detect } from "detect-package-manager"; | ||
export const findPackageJson = () => __awaiter(void 0, void 0, void 0, function* () { | ||
const rootPackageJson = yield rootPkgJSON(); | ||
const packageJson = yield packageDirectory().then((route) => route ? route + "/package.json" : undefined); | ||
const rootPackageJsonContent = rootPackageJson | ||
export const getPackageJsonScripts = (path) => __awaiter(void 0, void 0, void 0, function* () { | ||
const content = path | ||
? yield fs | ||
.readFile(rootPackageJson, "utf-8") | ||
.readFile(path, "utf-8") | ||
.then((content) => JSON.parse(content)) | ||
: undefined; | ||
const packageJsonContent = packageJson | ||
? yield fs | ||
.readFile(packageJson, "utf-8") | ||
.then((content) => JSON.parse(content)) | ||
: undefined; | ||
return { | ||
rootPackageJson, | ||
packageJson, | ||
rootPackageJsonContent, | ||
packageJsonContent, | ||
isRootPackageJson: rootPackageJson === packageJson, | ||
}; | ||
if (!content) | ||
return; | ||
if (!content.scripts || Object.keys(content.scripts).length === 0) { | ||
return; | ||
} | ||
return content.scripts; | ||
}); | ||
const run = () => __awaiter(void 0, void 0, void 0, function* () { | ||
const root = yield rootPkgJSON(); | ||
const packageManager = yield detect({ | ||
cwd: root === null || root === void 0 ? void 0 : root.replace("/package.json", ""), | ||
}); | ||
const data = yield findPackageJson(); | ||
const { scope } = data.isRootPackageJson | ||
? { scope: "monorepo" } | ||
: yield prompts({ | ||
type: "select", | ||
name: "scope", | ||
message: "Please choose the target for script execution:", | ||
choices: [ | ||
{ | ||
title: "Current package", | ||
selected: true, | ||
value: "closest", | ||
}, | ||
{ | ||
title: "Project root", | ||
value: "monorepo", | ||
}, | ||
], | ||
}); | ||
const scripts = scope === "closest" | ||
? data.packageJsonContent.scripts | ||
: data.rootPackageJsonContent.scripts; | ||
const selectAndRunScript = ({ packageManager, scripts, includeContextSwitch, }) => __awaiter(void 0, void 0, void 0, function* () { | ||
if (!scripts || Object.keys(scripts).length === 0) { | ||
console.log("No scripts found in package.json file."); | ||
return; | ||
} | ||
const choises = Object.entries(scripts).map(([key, value]) => ({ | ||
@@ -72,2 +42,9 @@ title: key, | ||
})); | ||
if (includeContextSwitch) { | ||
choises.unshift({ | ||
title: "Project root", | ||
description: "Change scope from package level to project level to pick script from project root package.json\n", | ||
value: "_root", | ||
}); | ||
} | ||
const { script } = yield prompts({ | ||
@@ -78,6 +55,56 @@ type: "autocomplete", | ||
choices: choises, | ||
active: "none", | ||
}); | ||
if (!script) { | ||
console.log("\nExited gracefully.\n"); | ||
process.exit(0); | ||
} | ||
if (script === "_root") { | ||
return true; | ||
} | ||
spawn(packageManager, ["run", script], { stdio: "inherit" }); | ||
return false; | ||
}); | ||
run(); | ||
export const main = () => __awaiter(void 0, void 0, void 0, function* () { | ||
const rootPackageJsonFilePath = yield rootPkgJSON(); | ||
const currentPackageJsonFilePath = yield packageDirectory().then((route) => route ? route + "/package.json" : undefined); | ||
if (!rootPackageJsonFilePath) { | ||
console.warn("We could not find root package.json. Check if the current path in the terminal is in an existing javascript project."); | ||
return; | ||
} | ||
const rootPackageJsonScripts = yield getPackageJsonScripts(rootPackageJsonFilePath); | ||
const currentPackageJsonScripts = yield getPackageJsonScripts(currentPackageJsonFilePath); | ||
const packageManager = yield detect({ | ||
cwd: rootPackageJsonFilePath.replace("/package.json", ""), | ||
}); | ||
if (currentPackageJsonScripts) { | ||
const switchToRoot = yield selectAndRunScript({ | ||
packageManager, | ||
scripts: currentPackageJsonScripts, | ||
includeContextSwitch: true, | ||
}); | ||
if (!switchToRoot) | ||
return; | ||
} | ||
if (currentPackageJsonFilePath !== rootPackageJsonFilePath && | ||
!currentPackageJsonScripts) { | ||
console.log("\n---------------------------------------"); | ||
console.log(`No scripts found in package.json file.\n`); | ||
console.log("Path of package.json file: ${currentPackageJsonFilePath}\n"); | ||
console.log("Will show root package scripts:"); | ||
console.log("---------------------------------------\n\n"); | ||
} | ||
if (!rootPackageJsonScripts) { | ||
console.log(`No scripts found in package.json file.\n\nPath of package.json file: ${currentPackageJsonFilePath}`); | ||
return; | ||
} | ||
if (rootPackageJsonScripts) { | ||
yield selectAndRunScript({ | ||
packageManager, | ||
scripts: rootPackageJsonScripts, | ||
includeContextSwitch: false, | ||
}); | ||
} | ||
}); | ||
main(); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "monorun", | ||
"version": "0.1.9", | ||
"version": "0.2.0", | ||
"description": "Intelligently detects and runs scripts specific to your project's directory in the terminal.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -51,2 +51,2 @@ # monorun | ||
This package is licensed under the MIT License. See the [LICENSE](LICENSE) file for details. | ||
This package is licensed under the MIT License. |
Sorry, the diff of this file is not supported yet
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
12923
8
136
2