Skip to content

Commit

Permalink
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 f9b2965 commit 4943b21
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ jobs:
- name: Local action test
uses: ./
id: versions
with:
fallbackNode: '1.2.3'
fallbackNpm: '3.2.1'

- name: Get versions
run: |
Expand Down
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ inputs:
required: false
description: 'Path of package.json'
default: "./"
fallbackNode:
required: false
description: 'Fallback if engines npm version is not defined'
fallbackNpm:
required: false
description: 'Fallback if engines npm version is not defined'
outputs:
nodeVersion:
description: "Node version from engines field in package.json"
Expand Down
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();
3 changes: 3 additions & 0 deletions src/__tests__/fixture-empty/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "fixture"
}
36 changes: 36 additions & 0 deletions src/__tests__/getNodeVersionEmpty.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import fs from 'fs';

import {
findPackageJson,
getNodeVersion,
getNpmVersion
} from '../getNodeVersion';

const fixturePath = './src/__tests__/fixture-empty';
const fixture = `./src/__tests__/fixture-empty/package.json`;

describe('getNodeVersion with fallback', () => {
describe('findPackageJson', () => {
test('find package.json', () => {
const result = findPackageJson(fixturePath);

expect(result).toBe(fs.readFileSync(fixture).toString());
});
});

describe('getNodeVersion', () => {
test('get undefined node version text within package.json with fallback', () => {
const result = getNodeVersion(fixturePath, '^12.22.1');

expect(result).toBe('^12.22.1');
});
});

describe('getNpmVersion', () => {
test('get undefined npm version text within package.json', () => {
const result = getNpmVersion(fixturePath);

expect(result).toBe('');
});
});
});
37 changes: 30 additions & 7 deletions src/getNodeVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,44 @@ export const findPackageJson = (path: string): string => {
};

/**
* Get engines node version field within package.json
* Get engines versions field within package.json
* @param type
* @param path
* @param fallback
*/
export const getNodeVersion = (path: string): string => {
const getEngineVersionFor = (
type: string,
path: string,
fallback?: string
): string => {
const packageJson = findPackageJson(path);
const engines = JSON.parse(packageJson).engines;

if (engines && engines[type]) {
return engines[type];
}

if (fallback) {
return fallback;
}

return JSON.parse(packageJson).engines.node;
return '';
};

/**
* Get engines npm version field within package.json
* Get engines node version field within package.json
* @param path
* @param fallback
*/
export const getNpmVersion = (path: string): string => {
const packageJson = findPackageJson(path);
export const getNodeVersion = (path: string, fallback?: string): string => {
return getEngineVersionFor('node', path, fallback);
};

return JSON.parse(packageJson).engines.npm;
/**
* Get engines npm version field within package.json
* @param path
* @param fallback
*/
export const getNpmVersion = (path: string, fallback?: string): string => {
return getEngineVersionFor('npm', path, fallback);
};
7 changes: 5 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import { getNodeVersion, getNpmVersion } from './getNodeVersion';
async function run() {
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(path);
const npmVersion = getNpmVersion(path);
const nodeVersion = getNodeVersion(path, fallbackNode);
const npmVersion = getNpmVersion(path, fallbackNpm);

core.debug(`nodeVersion: ${nodeVersion}, npmVersion: ${npmVersion}`);
core.setOutput('nodeVersion', nodeVersion);
Expand Down

0 comments on commit 4943b21

Please sign in to comment.