Skip to content
Open
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
31 changes: 0 additions & 31 deletions .circleci/config.yml

This file was deleted.

15 changes: 1 addition & 14 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
module.exports = {
extends: ['plugin:@typescript-eslint/recommended', '@ehacke/eslint-config', 'plugin:import/typescript'],
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint', 'import', 'simple-import-sort'],
rules: {
'@typescript-eslint/no-explicit-any': 'off',
},
settings: {
'import/resolver': {
node: {
extensions: ['.js', '.jsx', '.ts', '.tsx', '.d.ts', '.json'],
},
typescript: {},
},
},
extends: ['@ehacke/eslint-config-ts'],
};
81 changes: 81 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
name: ci

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:

test:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [10.x, 12.x, 14.x, 15.x]

steps:
- name: Checkout
uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}

- name: Cache node modules
uses: actions/cache@v2
env:
cache-name: cache-node-modules
with:
path: |
node_modules
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-

- name: Get dependencies
run: npm i

- name: Test
run: npm run test

- name: Coverage
uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }} # not required for public repos
file: coverage/lcov.info
fail_ci_if_error: true # optional (default = false)
verbose: true # optional (default = false)

lint:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [10.x, 12.x, 14.x, 15.x]

steps:
- name: Checkout
uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}

- name: Cache node modules
uses: actions/cache@v2
env:
cache-name: cache-node-modules
with:
path: |
node_modules
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-

- name: Get dependencies
run: npm i

- name: Lint
run: npm run lint
53 changes: 44 additions & 9 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import crypto from 'crypto';
import fs from 'fs-extra';
import os from 'os';
import path from 'path';
import tar, { ReadEntry } from 'tar';

import { Readable } from 'stream';
import { logSummary, packDirectory, PackSummaryInterface } from './lib/pack';

export { logSummary, PackSummaryInterface };
Expand All @@ -20,9 +22,10 @@ export interface PackResultInterface {
*
* @param {string} pkgDir
* @param {(result: PackResultInterface) => void} packageCallback
* @returns {Promise<void}
* @param {boolean} [cleanup=true]
* @returns {Promise<void>}
*/
export const createPackage = async (pkgDir: string, packageCallback: (result: PackResultInterface) => void): Promise<void> => {
export const createPackage = async (pkgDir: string, packageCallback: (result: PackResultInterface) => void, cleanup = true): Promise<void> => {
if (!pkgDir) {
throw new Error('pkgDir must be provided');
}
Expand All @@ -36,11 +39,43 @@ export const createPackage = async (pkgDir: string, packageCallback: (result: Pa
const tmpFolder = `pack-${process.pid}-${rand}`;
const tmpDir = path.resolve(os.tmpdir(), tmpFolder);

return Bluebird.resolve().then(async () => {
await fs.ensureDir(tmpDir);
const target = path.join(tmpDir, 'package.tgz');
const summary = await packDirectory(pkgDir, target);
return packageCallback({ target, summary });
});
// .finally(() => fs.remove(tmpDir));
return Bluebird.resolve()
.then(async () => {
await fs.ensureDir(tmpDir);
const target = path.join(tmpDir, 'package.tgz');
const summary = await packDirectory(pkgDir, target);
return packageCallback({ target, summary });
})
.finally(async () => (cleanup && (await fs.pathExists(tmpDir)) ? fs.remove(tmpDir) : null));
};

/**
* Extract package to specified directory
*
* @param {string} packageString
* @param {string} extractDir
* @returns {Promise<string[]>}
*/
export const extractLocal = async (packageString: string, extractDir: string): Promise<string[]> => {
const files = [] as string[];

const stream = new Readable();
stream.push(packageString, 'base64');
stream.push(null);
await new Promise<void>((resolve) =>
stream
.pipe(
tar.x({
C: extractDir,
onentry: (entry: ReadEntry) => {
if (entry.type === 'File') {
files.push(entry.path.replace(/^\.\//g, ''));
}
},
})
)
.on('close', (): void => resolve())
);

return files;
};
21 changes: 12 additions & 9 deletions lib/pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import fs from 'fs-extra';
import packlist from 'npm-packlist';
import log from 'npmlog';
import ssri from 'ssri';
import tar from 'tar';
import tar, { FileOptions, ListOptions } from 'tar';

export interface PackSummaryInterface {
size: number;
Expand All @@ -23,27 +23,30 @@ export const getSummary = async (target: string): Promise<PackSummaryInterface>
let totalEntries = 0;
let totalEntrySize = 0;

await tar.t({
const tarOpts: ListOptions & FileOptions = {
file: target,
onentry(entry) {
totalEntries++;
totalEntrySize += entry.size;
const p = entry.path;
// This type is wrong
const p = (entry.path as any) as string;
if (p.startsWith('package/node_modules/')) {
const name = p.match(/^package\/node_modules\/((?:@[^/]+\/)?[^/]+)/)[1];
if (bundledWanted.has(name)) {
const name = (p.match(/^package\/node_modules\/((?:@[^/]+\/)?[^/]+)/) || [])[1];
if (name && bundledWanted.has(name)) {
bundled.add(name);
}
} else {
files.push({
path: entry.path.replace(/^package\//, ''),
path: p.replace(/^package\//, ''),
size: entry.size,
mode: entry.mode,
mode: entry.mode as any,
});
}
},
strip: 1,
});
};

// Again, just a workaround for bad types. Strip is missing for some reason
await tar.t({ ...tarOpts, strip: 1 } as any);

const stat = await fs.stat(target);
const integrity = await ssri.fromStream(fs.createReadStream(target), {
Expand Down
Loading