Skip to content

Commit

Permalink
Merge pull request #2031 from zowe/dev/add-package-script
Browse files Browse the repository at this point in the history
Add script to build offline tgz for CLI package
  • Loading branch information
t1m0thyj authored Feb 5, 2024
2 parents bfac3d9 + 957c244 commit c5a99d8
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ CCS141/
src/brightside.iml
package-lock.json
/packages/cli/npm-shrinkwrap.json
/dist/
# Sonar Files
.sonar_lock
.scannerwork/
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"typedoc:packages": "lerna run --parallel typedoc",
"audit:public": "npm audit --registry https://registry.npmjs.org/",
"bundle:webHelp": "cd packages/imperative/web-help && node build.js",
"prepare": "husky install && npm run bundle:webHelp"
"prepare": "husky install && npm run bundle:webHelp",
"package": "node scripts/bundleCliTgz.js"
},
"dependencies": {},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const updateAndGetRemovedTypes = (npmPackage: string): string[] => {
}

return typesToRemove;
}
};

/**
* @TODO - allow multiple packages to be uninstalled?
Expand Down
49 changes: 49 additions & 0 deletions scripts/bundleCliTgz.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*
*/

const childProcess = require("child_process");
const fs = require("fs-extra");
const path = require("path");

// Workaround for https://github.com/npm/cli/issues/3466
process.chdir(__dirname + "/..");
const cliPkgDir = path.join(process.cwd(), "packages", "cli");
const pkgJsonFile = path.join(cliPkgDir, "package.json");
const execCmd = (cmd) => childProcess.execSync(cmd, { cwd: cliPkgDir, stdio: "inherit" });
fs.mkdirpSync("dist");
fs.renameSync(path.join(cliPkgDir, "node_modules"), path.join(cliPkgDir, "node_modules_old"));
fs.copyFileSync(pkgJsonFile, pkgJsonFile + ".bak");

try {
// Install node_modules directly inside packages/cli
execCmd("npm run preshrinkwrap");
execCmd("npm install --ignore-scripts --workspaces=false");
for (const zowePkgDir of fs.readdirSync(path.join(cliPkgDir, "node_modules", "@zowe"))) {
const srcDir = path.join("node_modules", "@zowe", zowePkgDir);
const destDir = path.join(cliPkgDir, srcDir);
fs.rmSync(destDir, { recursive: true, force: true });
fs.copySync(fs.realpathSync(srcDir), destDir);
}

// Define bundled dependencies in package.json and package the TGZ
const pkgJson = JSON.parse(fs.readFileSync(pkgJsonFile, "utf-8"));
pkgJson.bundledDependencies = [
...Object.keys(pkgJson.dependencies),
...Object.keys(pkgJson.optionalDependencies ?? {})
];
fs.writeFileSync(pkgJsonFile, JSON.stringify(pkgJson, null, 2));
execCmd("npm pack --pack-destination=../../dist");
} finally {
fs.rmSync(path.join(cliPkgDir, "node_modules"), { recursive: true, force: true });
fs.renameSync(path.join(cliPkgDir, "node_modules_old"), path.join(cliPkgDir, "node_modules"));
fs.rmSync(path.join(cliPkgDir, "npm-shrinkwrap.json"), { force: true });
fs.renameSync(pkgJsonFile + ".bak", pkgJsonFile);
}

0 comments on commit c5a99d8

Please sign in to comment.