| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| const fs = require("fs"); | ||
| const path = require("path"); | ||
|
|
||
| // dist/ is assembled as a complete package root, not just compiled output: | ||
| // lib/ resolves siblings through __dirname (../package.json, ../docs/helpers, | ||
| // ../../vendor/gradle-plugin, ...), so those directories have to sit next to it | ||
| // exactly as they do in the repo. | ||
|
|
||
| const rootDir = path.join(__dirname, ".."); | ||
| const distDir = path.join(rootDir, "dist"); | ||
| const release = process.argv.includes("--release"); | ||
|
|
||
| // bundleDependencies are resolved from node_modules next to the manifest being | ||
| // packed, so they have to be mirrored into dist for `npm pack` to bundle them | ||
| const BUNDLED = ["universal-analytics", "debug", "ms", "uuid"]; | ||
|
|
||
| const SIBLING_DIRS = ["resources", "docs", "config", "vendor", "bin", "setup"]; | ||
| // npm picks README/LICENSE/CHANGELOG up from the directory being packed, so | ||
| // they have to exist inside dist or they silently drop out of the tarball | ||
| const ROOT_FILES = [ | ||
| "postinstall.js", | ||
| "preuninstall.js", | ||
| "README.md", | ||
| "LICENSE", | ||
| "CHANGELOG.md", | ||
| ]; | ||
|
|
||
| // paths (relative to the repo root) that never ship | ||
| const RELEASE_EXCLUDES = [ | ||
| path.join("docs", "html"), | ||
| path.join("lib", "common", "docs", "fonts"), | ||
| path.join("lib", "common", "test"), | ||
| ]; | ||
|
|
||
| function isExcluded(relPath) { | ||
| if (!release) { | ||
| return false; | ||
| } | ||
| return RELEASE_EXCLUDES.some( | ||
| (excluded) => relPath === excluded || relPath.startsWith(excluded + path.sep) | ||
| ); | ||
| } | ||
|
|
||
| let copied = 0; | ||
| let skipped = 0; | ||
|
|
||
| function copyFile(sourcePath, targetPath) { | ||
| const source = fs.statSync(sourcePath); | ||
| if (fs.existsSync(targetPath)) { | ||
| const target = fs.statSync(targetPath); | ||
| // vendor/ alone is ~31MB; re-copying it on every build is pure waste | ||
| if (target.mtimeMs >= source.mtimeMs && target.size === source.size) { | ||
| skipped++; | ||
| return; | ||
| } | ||
| } | ||
| fs.mkdirSync(path.dirname(targetPath), { recursive: true }); | ||
| fs.copyFileSync(sourcePath, targetPath); | ||
| copied++; | ||
| } | ||
|
|
||
| function copyTree(relDir, filter) { | ||
| const sourceDir = path.join(rootDir, relDir); | ||
| if (!fs.existsSync(sourceDir)) { | ||
| return; | ||
| } | ||
|
|
||
| for (const entry of fs.readdirSync(sourceDir, { withFileTypes: true })) { | ||
| const relPath = path.join(relDir, entry.name); | ||
| if (isExcluded(relPath)) { | ||
| continue; | ||
| } | ||
| if (entry.isDirectory()) { | ||
| copyTree(relPath, filter); | ||
| } else if (!filter || filter(relPath)) { | ||
| copyFile(path.join(rootDir, relPath), path.join(distDir, relPath)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Everything under lib/ that is not TypeScript is an asset: vendored scripts, | ||
| // hooks, platform-tools binaries, docs helpers and test fixtures. A .js with a | ||
| // sibling .ts is compiler output instead - either left over from the old | ||
| // in-place build or freshly emitted into dist - and copying it would overwrite | ||
| // what tsc just produced. | ||
| function isCompilerOutput(relPath) { | ||
| const stem = relPath.replace(/\.js\.map$/, "").replace(/\.js$/, ""); | ||
| return ( | ||
| (relPath.endsWith(".js") || relPath.endsWith(".js.map")) && | ||
| fs.existsSync(path.join(rootDir, stem + ".ts")) | ||
| ); | ||
| } | ||
|
|
||
| // Hand-written .d.ts come along too. tsc treats them as inputs and never emits | ||
| // them, but the generated declarations import from them, so leaving them behind | ||
| // ships types with dangling references. | ||
| copyTree( | ||
| "lib", | ||
| (relPath) => | ||
| (!relPath.endsWith(".ts") || relPath.endsWith(".d.ts")) && | ||
| !isCompilerOutput(relPath) | ||
| ); | ||
|
|
||
| for (const dir of SIBLING_DIRS) { | ||
| copyTree(dir); | ||
| } | ||
|
|
||
| if (!release) { | ||
| // fixtures the compiled tests read relative to their own location | ||
| copyTree(path.join("test", "files")); | ||
| } | ||
|
|
||
| for (const file of ROOT_FILES) { | ||
| copyFile(path.join(rootDir, file), path.join(distDir, file)); | ||
| } | ||
|
|
||
| for (const dep of BUNDLED) { | ||
| copyTree(path.join("node_modules", dep)); | ||
| } | ||
|
|
||
| writeManifest(); | ||
|
|
||
| function writeManifest() { | ||
| const pkg = JSON.parse( | ||
| fs.readFileSync(path.join(rootDir, "package.json"), "utf8") | ||
| ); | ||
|
|
||
| // dist is the package root once published, so entrypoints lose the dist/ | ||
| // prefix they carry in the source manifest | ||
| pkg.main = pkg.main.replace(/^\.\/dist\//, "./"); | ||
|
|
||
| delete pkg.devDependencies; | ||
| delete pkg.files; | ||
| delete pkg.overrides; | ||
| delete pkg["lint-staged"]; | ||
|
|
||
| pkg.scripts = { | ||
| postinstall: pkg.scripts.postinstall, | ||
| preuninstall: pkg.scripts.preuninstall, | ||
| }; | ||
|
|
||
| fs.writeFileSync( | ||
| path.join(distDir, "package.json"), | ||
| JSON.stringify(pkg, null, 2) + "\n" | ||
| ); | ||
| } | ||
|
|
||
| console.log( | ||
| `assets: ${copied} copied, ${skipped} up to date${release ? " (release)" : ""}` | ||
| ); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| // The published package is assembled in dist/ and packed from there, so packing | ||
| // the repository root would produce a tarball with everything nested one level | ||
| // deeper - every path the CLI resolves through __dirname would break, silently. | ||
| // npm pack ./dist runs dist's own manifest, so this guard does not fire for it. | ||
| console.error( | ||
| [ | ||
| "Refusing to pack the repository root.", | ||
| "", | ||
| "The published package is assembled in dist/. Use:", | ||
| " npm run pack", | ||
| "", | ||
| "which builds dist/ and runs `npm pack ./dist`.", | ||
| ].join("\n") | ||
| ); | ||
|
|
||
| process.exit(1); |
| Back | FazBrowse Home | New Git URL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain🏁 Script executed:
Repository: NativeScript/nativescript-cli
Length of output: 1829
Validate the --dir value before resolving it.
node scripts/set-ga-id.js live --dir passes undefined to path.resolve, throwing before the usage branch handles invalid input. Gate --dir on a non-empty string or --* option and use that target value in path.resolve.
Proposed fixconst dirIndex = process.argv.indexOf("--dir"); +const targetDir = dirIndex === -1 ? undefined : process.argv[dirIndex + 1]; +if (dirIndex !== -1 && (!targetDir || targetDir.startsWith("--"))) { + console.error( + "Usage: node scripts/set-ga-id.js <live|dev|verify> [--dir <path>]" + ); + process.exit(1); +} const baseDir = - dirIndex === -1 ? rootDir : path.resolve(rootDir, process.argv[dirIndex + 1]); + targetDir === undefined ? rootDir : path.resolve(rootDir, targetDir);Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.