From 4420514aad79cf98dce7f23287b661c0fcd5301b Mon Sep 17 00:00:00 2001 From: Matt Glaman Date: Mon, 19 Apr 2021 14:39:59 -0500 Subject: [PATCH] Ensure installation path is resolved correctly (#1) Node.js doesn't resolve `~` automatically. --- .github/workflows/workflow.yml | 13 ++++++++++++- __tests__/index.test.js | 4 ++++ dist/index.js | 12 +++++++++++- src/index.js | 2 +- src/utils.js | 10 ++++++++++ 5 files changed, 38 insertions(+), 3 deletions(-) diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index 5c7bfb9..e3d04f3 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -12,6 +12,18 @@ on: paths-ignore: - '**.md' jobs: + test: + name: Test + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Use Node.js + uses: actions/setup-node@v1 + with: + node-version: 15.x + - run: npm ci + - run: npm test + run: name: Run runs-on: ${{ matrix.operating-system }} @@ -55,6 +67,5 @@ jobs: with: version: ${{ matrix.drupal-version }} path: ~/drupal - - name: Verify build run: test -f ~/drupal/web/index.php diff --git a/__tests__/index.test.js b/__tests__/index.test.js index 8b78573..c4110e7 100644 --- a/__tests__/index.test.js +++ b/__tests__/index.test.js @@ -1,5 +1,9 @@ const utils = require ('../src/utils') +test('resolved path', () => { + expect(utils.resolvePath('~/drupal')).toBe(`${process.env.HOME}/drupal`); +}); + test('can find major version from constraint', () => { expect(utils.getMajorVersionFromConstraint('^9.1')).toBe(9); expect(utils.getMajorVersionFromConstraint('^9.2@alpha')).toBe(9); diff --git a/dist/index.js b/dist/index.js index 9082a72..9cae8b3 100644 --- a/dist/index.js +++ b/dist/index.js @@ -2240,7 +2240,7 @@ const utils = __nccwpck_require__ (608) async function doScript() { const drupalVersion = core.getInput('version'); - const drupalPath = core.getInput('path'); + const drupalPath = utils.resolvePath(core.getInput('path') || '~/drupal'); const extraDependencies = core.getInput('dependencies') await exec.exec('composer', [ @@ -2282,14 +2282,24 @@ doScript().catch(error => core.setFailed(error.message)); /***/ 608: /***/ ((module, __unused_webpack_exports, __nccwpck_require__) => { +const path = __nccwpck_require__(622); const semverMajor = __nccwpck_require__(688) const smeverCoerce = __nccwpck_require__(466) +function resolvePath(filepath) { + if (filepath[0] === '~') { + return path.join(process.env.HOME, filepath.slice(1)); + } + return path.resolve(filepath) +} + function getMajorVersionFromConstraint(constraint) { return semverMajor(smeverCoerce(constraint)) } + module.exports = { + resolvePath, getMajorVersionFromConstraint } diff --git a/src/index.js b/src/index.js index 8d70a2f..49b93f3 100644 --- a/src/index.js +++ b/src/index.js @@ -4,7 +4,7 @@ const utils = require ('./utils') async function doScript() { const drupalVersion = core.getInput('version'); - const drupalPath = core.getInput('path'); + const drupalPath = utils.resolvePath(core.getInput('path') || '~/drupal'); const extraDependencies = core.getInput('dependencies') await exec.exec('composer', [ diff --git a/src/utils.js b/src/utils.js index 4c51923..1eac0dc 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,10 +1,20 @@ +const path = require('path'); const semverMajor = require('semver/functions/major') const smeverCoerce = require('semver/functions/coerce') +function resolvePath(filepath) { + if (filepath[0] === '~') { + return path.join(process.env.HOME, filepath.slice(1)); + } + return path.resolve(filepath) +} + function getMajorVersionFromConstraint(constraint) { return semverMajor(smeverCoerce(constraint)) } + module.exports = { + resolvePath, getMajorVersionFromConstraint }