Skip to content

Commit

Permalink
Merge pull request #3 from skjnldsv/feat/fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
skjnldsv authored Jun 18, 2021
2 parents f9b2965 + fe94c40 commit 1e2f46e
Show file tree
Hide file tree
Showing 10 changed files with 191 additions and 51 deletions.
61 changes: 32 additions & 29 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,40 @@ on:
push:
branches:
- master
- 'releases/*'
- "releases/*"

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- uses: actions/cache@v2
id: cache
with:
path: node_modules
key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-npm-
- name: Install dependencies
if: steps.cache.outputs.cache-hit != 'true'
run: npm ci

- name: Build
run: npm run package

- name: Unit Test
run: npm test

- name: Local action test
uses: ./
id: versions

- name: Get versions
run: |
echo "Node version is ${{ steps.versions.outputs.nodeVersion }}"
echo "Npm version is ${{ steps.versions.outputs.npmVersion }}"
- uses: actions/checkout@v2

- uses: actions/cache@v2
id: cache
with:
path: node_modules
key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-npm-
- name: Install dependencies
if: steps.cache.outputs.cache-hit != 'true'
run: npm ci

- name: Build
run: npm run build

- name: Unit Test
run: npm test

- name: Package
run: npm run package

- name: Local action test
uses: ./
id: versions

- name: Get versions
run: |
echo "Node version is ${{ steps.versions.outputs.nodeVersion }}"
echo "Npm version is ${{ steps.versions.outputs.npmVersion }}"
14 changes: 10 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
__tests__/runner/*

# comment out in distribution branches
node_modules/
# Dependency directory
node_modules

# Rest pulled from https://github.com/github/gitignore/blob/master/Node.gitignore
# Logs
Expand Down Expand Up @@ -91,3 +89,11 @@ typings/

# DynamoDB Local files
.dynamodb/

# OS metadata
.DS_Store
Thumbs.db

# Ignore built ts files
__tests__/runner/*
lib/**/*
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,46 @@ jobs:
run: echo "Npm version is ${{ steps.package-engines-versions.outputs.npmVersion }}"
# Version is ^6.1.3
```
### fallbackNode, fallbackNpm

`fallbackNode` and `fallbackNpm` allows you to define a fallback value if not defined

```json

{
"name": "your-package",
"engines": {
}
}
```

```yml
name: Get node and npm versions from package.json

on: push

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Read node and npm versions from package.json
uses: skjnldsv/read-package-engines-version-actions@v1
with:
fallbackNode: '^14'
fallbackNpm: '^6'
id: package-engines-versions

- name: Show node version number
run: echo "Node version is ${{ steps.package-engines-versions.outputs.nodeVersion }}"
# Version is ^14

- name: Show npm version number
run: echo "Npm version is ${{ steps.package-engines-versions.outputs.npmVersion }}"
# Version is ^6
```

# License

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
36 changes: 28 additions & 8 deletions dist/index.js

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

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

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 1e2f46e

Please sign in to comment.