Skip to content

Commit

Permalink
fixup! Allow fallback if undefined
Browse files Browse the repository at this point in the history
Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  • Loading branch information
skjnldsv committed Jun 18, 2021
1 parent a8779e3 commit d2823f0
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
47 changes: 47 additions & 0 deletions lib/getNodeVersion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const path_1 = require("path");
/**
* Find package.json with path.
* @param path
*/
exports.findPackageJson = (path) => {
return fs_1.default.readFileSync(path_1.join(path, 'package.json')).toString();
};
/**
* Get engines versions field within package.json
* @param type
* @param path
* @param fallback
*/
const getEngineVersionFor = (type, path, fallback) => {
const packageJson = exports.findPackageJson(path);
const engines = JSON.parse(packageJson).engines;
if (engines && engines[type]) {
return engines[type];
}
if (fallback) {
return fallback;
}
return '';
};
/**
* Get engines node version field within package.json
* @param path
* @param fallback
*/
exports.getNodeVersion = (path, fallback) => {
return getEngineVersionFor('node', path, fallback);
};
/**
* Get engines npm version field within package.json
* @param path
* @param fallback
*/
exports.getNpmVersion = (path, fallback) => {
return getEngineVersionFor('npm', path, fallback);
};
39 changes: 39 additions & 0 deletions lib/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const core = __importStar(require("@actions/core"));
const getNodeVersion_1 = require("./getNodeVersion");
function run() {
return __awaiter(this, void 0, void 0, function* () {
try {
const path = core.getInput('path');
const fallbackNode = core.getInput('fallbackNode');
const fallbackNpm = core.getInput('fallbackNpm');
core.debug(`Load package.json at ${path}`);
core.debug(`Fallback to ${fallbackNode} / ${fallbackNpm} if undefined`);
const nodeVersion = getNodeVersion_1.getNodeVersion(path, fallbackNode);
const npmVersion = getNodeVersion_1.getNpmVersion(path, fallbackNpm);
core.debug(`nodeVersion: ${nodeVersion}, npmVersion: ${npmVersion}`);
core.setOutput('nodeVersion', nodeVersion);
core.setOutput('npmVersion', npmVersion);
}
catch (error) {
core.setFailed(error.message);
}
});
}
run();

0 comments on commit d2823f0

Please sign in to comment.