Improve prepack script (#1747)

* improve prepack script

* remove .npmignore

* make prepack use pkg.files

* fix linter errors

(cherry picked from commit cc19ccfdb3)
This commit is contained in:
Francisco Giordano
2019-05-14 16:04:40 -03:00
committed by Francisco Giordano
parent b7b8fa947e
commit ee7ff81728
6 changed files with 222 additions and 112 deletions

View File

@ -1,26 +0,0 @@
#!/usr/bin/env bash
# Configure to exit script as soon as a command fails.
set -o errexit
# Clean the existing build directory.
rm -rf build
# Create a temporary directory to place ignored files (e.g. examples).
tmp_dir="ignored_contracts"
mkdir "$tmp_dir"
# Move the ignored files to the temporary directory.
while IFS="" read -r ignored
do
mv "contracts/$ignored" "$tmp_dir"
done < contracts/.npmignore
# Compile everything else.
npm run compile
# Return ignored files to their place.
mv "$tmp_dir/"* contracts/
# Delete the temporary directory.
rmdir "$tmp_dir"

10
scripts/compile.sh Executable file
View File

@ -0,0 +1,10 @@
#!/usr/bin/env sh
if [ "$SOLC_NIGHTLY" = true ]; then
docker pull ethereum/solc:nightly
fi
# Necessary to avoid an error in Truffle
rm -rf build/contracts
truffle compile

42
scripts/prepack.js Normal file
View File

@ -0,0 +1,42 @@
#!/usr/bin/env node
// This script removes the build artifacts of ignored contracts.
const fs = require('fs');
const path = require('path');
const cp = require('child_process');
const match = require('micromatch');
function exec (cmd, ...args) {
cp.execFileSync(cmd, args, { stdio: 'inherit' });
}
function readJSON (path) {
return JSON.parse(fs.readFileSync(path));
}
exec('npm', 'run', 'compile');
const pkgFiles = readJSON('package.json').files;
// Get only negated patterns.
const ignorePatterns = pkgFiles
.filter(pat => pat.startsWith('!'))
// Add **/* to ignore all files contained in the directories.
.flatMap(pat => [pat, path.join(pat, '**/*')])
// Remove the negation part. Makes micromatch usage more intuitive.
.map(pat => pat.slice(1));
const artifactsDir = 'build/contracts';
for (const artifact of fs.readdirSync(artifactsDir)) {
const fullArtifactPath = path.join(artifactsDir, artifact);
const { sourcePath: fullSourcePath } = readJSON(fullArtifactPath);
const sourcePath = path.relative('.', fullSourcePath);
const ignore = match.any(sourcePath, ignorePatterns);
if (ignore) {
fs.unlinkSync(fullArtifactPath);
}
}