Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: cache dependencies #13

Draft
wants to merge 6 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 64 additions & 19 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }; }

Expand All @@ -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 : {};

Expand Down Expand Up @@ -6308,7 +6310,7 @@ var dist = createCommonjsModule(function (module, exports) {
*/

var res = '';
var cache;
var cache$1;

/**
* Expose `repeat`
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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/)');
Expand All @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
19 changes: 15 additions & 4 deletions pnpm-lock.yaml

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

2 changes: 2 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -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,
}),
Expand Down
78 changes: 62 additions & 16 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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/)');
Expand All @@ -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) {
Expand Down