Skip to content

Commit

Permalink
[INTERNAL] Fix invoking local CLI installation (#548)
Browse files Browse the repository at this point in the history
Also adds a basic e2e test to verify the functionality.
  • Loading branch information
matz3 committed Nov 3, 2022
1 parent 07f0a60 commit 5c4611e
Show file tree
Hide file tree
Showing 18 changed files with 569 additions and 250 deletions.
62 changes: 62 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# CI (Azure Pipelines) xUnit test results
test-results.xml

# IDEs
.vscode/
*.~vsdx
.idea/

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules
jspm_packages

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# Misc
yarn.lock
.DS_Store

# Don't include private SSH key for deployment via Travis CI
deploy_key

# Custom directories
test/tmp/
jsdocs/
17 changes: 16 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = {
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2022
},
"env": {
"node": true,
Expand Down Expand Up @@ -82,5 +83,19 @@ module.exports = {
}
}
},
"root": true
"root": true,
"overrides": [
{
"files": [
"bin/ui5.cjs"
],
"parserOptions": {
"sourceType": "script",
},
"env": {
"es2022": false, // disable ES2022 globals
"es2020": true // first version that supports the 'import' keyword
}
}
]
};
6 changes: 6 additions & 0 deletions .github/workflows/github-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ jobs:
uses: coverallsapp/github-action@1.1.3
with:
github-token: ${{ secrets.GITHUB_TOKEN }}

- name: Build e2e test image
run: ./test/e2e/build-image.sh

- name: Run e2e test image
run: ./test/e2e/run-image.sh
104 changes: 104 additions & 0 deletions bin/ui5.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#!/usr/bin/env node

// NOTE: This file should be compatible to as many Node.js versions as possible
// so that the message for unsupported Node.js versions can be displayed.

const path = require("path");

const ui5 = {

getPackageJson() {
return require("../package.json");
},

checkRequirements({pkg, nodeVersion}) {
let semver;
try {
semver = require("semver");
} catch (err) {
// SyntaxError indicates an outdated Node.js version
if (err.name !== "SyntaxError") {
throw err;
}
}
if (
pkg.engines && pkg.engines.node &&
(!semver || !semver.satisfies(nodeVersion, pkg.engines.node, {includePrerelease: true}))
) {
console.log("==================== UNSUPPORTED NODE.JS VERSION ====================");
console.log("You are using an unsupported version of Node.js");
console.log("Detected version " + nodeVersion + " but " + pkg.name + " requires " + pkg.engines.node);
console.log("");
console.log("=> Please upgrade to a supported version of Node.js to use this tool");
console.log("=====================================================================");
return false;
}

if (semver && semver.prerelease(nodeVersion)) {
console.log("====================== UNSTABLE NODE.JS VERSION =====================");
console.log("You are using an unstable version of Node.js");
console.log("Detected Node.js version " + nodeVersion);
console.log("");
console.log("=> Please note that an unstable version might cause unexpected");
console.log(" behavior. For productive use please consider using a stable");
console.log(" version of Node.js! For the release policy of Node.js, see");
console.log(" https://nodejs.org/en/about/releases");
console.log("=====================================================================");
}

return true;
},

async invokeLocalInstallation(pkg) {
if (process.env.UI5_CLI_NO_LOCAL) {
return false;
}
// Prefer a local installation of @ui5/cli.
// This will invoke the local CLI, so no further action required
// NOTE: Using ui5.js, NOT ui5.cjs to also support CLI versions < v3.
// Starting with v3, both extensions can be used (see package.json "exports").
const {default: importLocal} = await import("import-local");
const ui5Local = importLocal(path.join(__dirname, "ui5.js"));
if (!ui5Local || ui5Local === module.exports) {
// Either no local installation found or this script is the local installation
// (invocation within ui5-cli repo)
return false;
}
if (process.argv.includes("--verbose")) {
console.info(`INFO: This project contains an individual ${pkg.name} installation which ` +
"will be used over the global one.");
console.info("See https://github.com/SAP/ui5-cli#local-vs-global-installation for details.");
console.info("");
} else {
console.info(`INFO: Using local ${pkg.name} installation`);
console.info("");
}
return true;
},

async invokeCLI(pkg) {
const {default: cli} = await import("../lib/cli/cli.js");
await cli(pkg);
},

async main() {
const pkg = ui5.getPackageJson();
if (!ui5.checkRequirements({pkg, nodeVersion: process.version})) {
return;
}
const localInstallationInvoked = await ui5.invokeLocalInstallation(pkg);
if (!localInstallationInvoked) {
await ui5.invokeCLI(pkg);
}
}
};

module.exports = ui5;

if (process.env.NODE_ENV !== "test" || process.env.UI5_CLI_TEST_BIN_RUN_MAIN !== "false") {
ui5.main().catch((err) => {
console.log("Fatal Error: Unable to initialize UI5 CLI");
console.log(err);
process.exit(1);
});
}
62 changes: 0 additions & 62 deletions bin/ui5.js

This file was deleted.

2 changes: 1 addition & 1 deletion lib/cli/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default async (pkg) => {
// Explicitly set CLI version as the yargs default might
// be wrong in case a local CLI installation is used
// Also add CLI location
const ui5JsPath = fileURLToPath(new URL("../../bin/ui5.js", import.meta.url));
const ui5JsPath = fileURLToPath(new URL("../../bin/ui5.cjs", import.meta.url));
const pkgVersion = `${pkg.version} (from ${ui5JsPath})`;

setVersion(pkgVersion);
Expand Down
39 changes: 9 additions & 30 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
"tool"
],
"bin": {
"ui5": "./bin/ui5.js"
"ui5": "./bin/ui5.cjs"
},
"type": "module",
"exports": {
"./bin/ui5.js": "./bin/ui5.cjs",
"./bin/ui5.cjs": "./bin/ui5.cjs",
"./package.json": "./package.json"
},
"engines": {
Expand Down Expand Up @@ -140,7 +142,7 @@
"eslint-config-google": "^0.14.0",
"eslint-plugin-ava": "^13.0.2",
"eslint-plugin-jsdoc": "^37.6.3",
"esmock": "^2.0.0",
"esmock": "^2.0.7",
"execa": "^5.1.1",
"jsdoc": "^3.6.7",
"nyc": "^15.1.0",
Expand Down

0 comments on commit 5c4611e

Please sign in to comment.