diff --git a/dist/index.js b/dist/index.js index 9192615..514e20a 100644 --- a/dist/index.js +++ b/dist/index.js @@ -14,6 +14,7 @@ var Stream = require('stream'); var Url = require('url'); var zlib = require('zlib'); var childProcess = require('child_process'); +var crypto = require('crypto'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } @@ -30,6 +31,7 @@ var Stream__default = /*#__PURE__*/_interopDefaultLegacy(Stream); var Url__default = /*#__PURE__*/_interopDefaultLegacy(Url); var zlib__default = /*#__PURE__*/_interopDefaultLegacy(zlib); var childProcess__default = /*#__PURE__*/_interopDefaultLegacy(childProcess); +var crypto__default = /*#__PURE__*/_interopDefaultLegacy(crypto); var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; @@ -6308,7 +6310,7 @@ var dist = createCommonjsModule(function (module, exports) { */ var res = ''; -var cache; +var cache$1; /** * Expose `repeat` @@ -6344,8 +6346,8 @@ function repeat(str, num) { if (num === 2) return str + str; var max = str.length * num; - if (cache !== str || typeof cache === 'undefined') { - cache = str; + if (cache$1 !== str || typeof cache$1 === 'undefined') { + cache$1 = str; res = ''; } else if (res.length >= max) { return res.substr(0, max); @@ -10819,6 +10821,7 @@ async function upsertComment({ core.endGroup(); } +console.log('dirname', __dirname); // import {createTempDirectory} from '@actions/cache/lib/internal/cacheUtils'; const COMMENT_SIGNATURE = sub('🤖 This report was automatically generated by [pkg-size-action](https://github.com/privatenumber/pkg-size-action/)'); @@ -10839,33 +10842,75 @@ async function isBaseDiffFromHead(baseRef) { return exitCode !== 0; } +const lockFiles = { + 'package-lock.json': 'npm ci', + 'yarn.lock': 'yarn install --frozen-lockfile', // yarn is installed on GitHub Actions by default + 'pnpm-lock.yaml': 'npx pnpm i --frozen-lockfile', // pnpm is not installed on GitHub Actions by default +}; + +function findLockFile(directory) { + for (const lockFile in lockFiles) { // eslint-disable-line guard-for-in + const lockFilePath = path__default['default'].join(directory, lockFile); + if (fs__default['default'].existsSync(lockFilePath)) { + return { + lockFilePath, + command: lockFiles[lockFile], + }; + } + } + + return { + lockFilePath: undefined, + command: 'npm i', + }; +} + +const getHash = filePath => new Promise(resolve => { + const hash = crypto__default['default'].createHash('md5'); + fs__default['default'].createReadStream(filePath) + .on('data', data => hash.update(data)) + .on('end', () => resolve(hash.digest('hex'))); +}); + async function npmCi({cwd} = {}) { if (fs__default['default'].existsSync('node_modules')) { core.info('Cleaning node_modules'); await rmRF_1(path__default['default'].join(cwd, 'node_modules')); } - if (fs__default['default'].existsSync('package-lock.json')) { - core.info('Installing dependencies with npm'); - return await exec$2('npm ci', {cwd}); - } + const packageManager = findLockFile(cwd); - if (fs__default['default'].existsSync('yarn.lock')) { - core.info('Installing dependencies with yarn'); + console.log(JSON.stringify(packageManager, null, 4)); - // yarn is installed on GitHub Actions by default - return await exec$2('yarn install --frozen-lockfile', {cwd}); - } + const packageLockHash = await getHash(packageManager.lockFilePath); - if (fs__default['default'].existsSync('pnpm-lock.yaml')) { - core.info('Installing dependencies with pnpm'); + const cached = await cache.saveCache(['node_modules'], packageLockHash); + console.log('cached', JSON.stringify(cached, null, 4)); - // pnpm is not installed on GitHub Actions by default - return await exec$2('npx pnpm i --frozen-lockfile', {cwd}); - } + core.info(`Installing dependencies with ${packageManager.command}`); + return await exec$2(packageManager.command, {cwd}); + + // if (fs.existsSync('package-lock.json')) { + // core.info('Installing dependencies with npm'); + // return await exec('npm ci', {cwd}); + // } + + // if (fs.existsSync('yarn.lock')) { + // core.info('Installing dependencies with yarn'); + + // // yarn is installed on GitHub Actions by default + // return await exec('yarn install --frozen-lockfile', {cwd}); + // } + + // if (fs.existsSync('pnpm-lock.yaml')) { + // core.info('Installing dependencies with pnpm'); + + // // pnpm is not installed on GitHub Actions by default + // return await exec('npx pnpm i --frozen-lockfile', {cwd}); + // } - core.info('No lock file detected. Installing dependencies with npm'); - return await exec$2('npm i', {cwd}); + // core.info('No lock file detected. Installing dependencies with npm'); + // return await exec('npm i', {cwd}); } async function isFileTracked(filePath) { diff --git a/package.json b/package.json index b88d65a..d57ad1a 100644 --- a/package.json +++ b/package.json @@ -38,12 +38,13 @@ ] }, "devDependencies": { - "@actions/cache": "^1.0.4", + "@actions/cache": "^1.0.5", "@actions/core": "^1.2.6", "@actions/exec": "^1.0.4", "@actions/github": "^4.0.0", "@actions/io": "^1.0.2", "@rollup/plugin-commonjs": "^17.0.0", + "@rollup/plugin-json": "^4.1.0", "@rollup/plugin-node-resolve": "^11.0.0", "builtin-modules": "^3.1.0", "byte-size": "^7.0.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0224d31..12241c8 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1,10 +1,11 @@ devDependencies: - '@actions/cache': 1.0.4 + '@actions/cache': 1.0.5 '@actions/core': 1.2.6 '@actions/exec': 1.0.4 '@actions/github': 4.0.0 '@actions/io': 1.0.2 '@rollup/plugin-commonjs': 17.0.0_rollup@2.34.2 + '@rollup/plugin-json': 4.1.0_rollup@2.34.2 '@rollup/plugin-node-resolve': 11.0.0_rollup@2.34.2 builtin-modules: 3.1.0 byte-size: 7.0.0 @@ -18,7 +19,7 @@ devDependencies: xo: 0.35.0 lockfileVersion: 5.2 packages: - /@actions/cache/1.0.4: + /@actions/cache/1.0.5: dependencies: '@actions/core': 1.2.6 '@actions/exec': 1.0.4 @@ -31,7 +32,7 @@ packages: uuid: 3.4.0 dev: true resolution: - integrity: sha512-1grYfbu8P6JDDHc40eOI5tQDRcAxMwq5HBWhaCqEg9o/ixDRZfwPHlQvQAop2ZzFCjF2ns0ENQOIBAH8GNn+zA== + integrity: sha512-TcvJOduwsPP27KLmIa5cqXsQYFK2GzILcEpnhvYmhGwi1aYx9XwhKmp6Im8X6DJMBxbvupKPsOntG6f6sSkIPA== /@actions/core/1.2.6: dev: true resolution: @@ -455,6 +456,15 @@ packages: rollup: ^2.30.0 resolution: integrity: sha512-/omBIJG1nHQc+bgkYDuLpb/V08QyutP9amOrJRUSlYJZP+b/68gM//D8sxJe3Yry2QnYIr3QjR3x4AlxJEN3GA== + /@rollup/plugin-json/4.1.0_rollup@2.34.2: + dependencies: + '@rollup/pluginutils': 3.1.0_rollup@2.34.2 + rollup: 2.34.2 + dev: true + peerDependencies: + rollup: ^1.20.0 || ^2.0.0 + resolution: + integrity: sha512-yfLbTdNS6amI/2OpmbiBoW12vngr5NW2jCJVZSBEz+H5KfUJZ2M7sDjk0U6GOOdCWFVScShte29o9NezJ53TPw== /@rollup/plugin-node-resolve/11.0.0_rollup@2.34.2: dependencies: '@rollup/pluginutils': 3.1.0_rollup@2.34.2 @@ -5366,12 +5376,13 @@ packages: resolution: integrity: sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q== specifiers: - '@actions/cache': ^1.0.4 + '@actions/cache': ^1.0.5 '@actions/core': ^1.2.6 '@actions/exec': ^1.0.4 '@actions/github': ^4.0.0 '@actions/io': ^1.0.2 '@rollup/plugin-commonjs': ^17.0.0 + '@rollup/plugin-json': ^4.1.0 '@rollup/plugin-node-resolve': ^11.0.0 builtin-modules: ^3.1.0 byte-size: ^7.0.0 diff --git a/rollup.config.js b/rollup.config.js index a1bb469..03a8137 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,11 +1,13 @@ import commonjs from '@rollup/plugin-commonjs'; import {nodeResolve} from '@rollup/plugin-node-resolve'; +import json from '@rollup/plugin-json'; import builtins from 'builtin-modules'; const rollupConfig = { input: 'src/index.js', plugins: [ commonjs(), + json(), nodeResolve({ preferBuiltins: false, }), diff --git a/src/index.js b/src/index.js index 22e0d9f..b308e7c 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,8 @@ import * as core from '@actions/core'; import {context} from '@actions/github'; import {rmRF} from '@actions/io'; +// import cache from '@actions/cache'; +import crypto from 'crypto'; import assert from 'assert'; import fs from 'fs'; import path from 'path'; @@ -10,6 +12,8 @@ import {sub} from './utils/markdown'; import comparePackages from './utils/compare-packages'; import upsertComment from './utils/upsert-comment'; + +console.log('dirname', __dirname); // import {createTempDirectory} from '@actions/cache/lib/internal/cacheUtils'; const COMMENT_SIGNATURE = sub('🤖 This report was automatically generated by [pkg-size-action](https://github.com/privatenumber/pkg-size-action/)'); @@ -30,33 +34,75 @@ async function isBaseDiffFromHead(baseRef) { return exitCode !== 0; } +const lockFiles = { + 'package-lock.json': 'npm ci', + 'yarn.lock': 'yarn install --frozen-lockfile', // yarn is installed on GitHub Actions by default + 'pnpm-lock.yaml': 'npx pnpm i --frozen-lockfile', // pnpm is not installed on GitHub Actions by default +}; + +function findLockFile(directory) { + for (const lockFile in lockFiles) { // eslint-disable-line guard-for-in + const lockFilePath = path.join(directory, lockFile); + if (fs.existsSync(lockFilePath)) { + return { + lockFilePath, + command: lockFiles[lockFile], + }; + } + } + + return { + lockFilePath: undefined, + command: 'npm i', + }; +} + +const getHash = filePath => new Promise(resolve => { + const hash = crypto.createHash('md5'); + fs.createReadStream(filePath) + .on('data', data => hash.update(data)) + .on('end', () => resolve(hash.digest('hex'))); +}); + async function npmCi({cwd} = {}) { if (fs.existsSync('node_modules')) { core.info('Cleaning node_modules'); await rmRF(path.join(cwd, 'node_modules')); } - if (fs.existsSync('package-lock.json')) { - core.info('Installing dependencies with npm'); - return await exec('npm ci', {cwd}); - } + const packageManager = findLockFile(cwd); - if (fs.existsSync('yarn.lock')) { - core.info('Installing dependencies with yarn'); + console.log(JSON.stringify(packageManager, null, 4)); - // yarn is installed on GitHub Actions by default - return await exec('yarn install --frozen-lockfile', {cwd}); - } + // const packageLockHash = await getHash(packageManager.lockFilePath); - if (fs.existsSync('pnpm-lock.yaml')) { - core.info('Installing dependencies with pnpm'); + // const cached = await cache.saveCache(['node_modules'], packageLockHash); + // console.log('cached', JSON.stringify(cached, null, 4)); - // pnpm is not installed on GitHub Actions by default - return await exec('npx pnpm i --frozen-lockfile', {cwd}); - } + core.info(`Installing dependencies with ${packageManager.command}`); + return await exec(packageManager.command, {cwd}); + + // if (fs.existsSync('package-lock.json')) { + // core.info('Installing dependencies with npm'); + // return await exec('npm ci', {cwd}); + // } + + // if (fs.existsSync('yarn.lock')) { + // core.info('Installing dependencies with yarn'); + + // // yarn is installed on GitHub Actions by default + // return await exec('yarn install --frozen-lockfile', {cwd}); + // } + + // if (fs.existsSync('pnpm-lock.yaml')) { + // core.info('Installing dependencies with pnpm'); + + // // pnpm is not installed on GitHub Actions by default + // return await exec('npx pnpm i --frozen-lockfile', {cwd}); + // } - core.info('No lock file detected. Installing dependencies with npm'); - return await exec('npm i', {cwd}); + // core.info('No lock file detected. Installing dependencies with npm'); + // return await exec('npm i', {cwd}); } async function isFileTracked(filePath) {