forked from culshaw/read-package-node-version-actions
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
- Loading branch information
Showing
8 changed files
with
169 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"name": "fixture" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(''); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters