Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
changedFilesOutsideScope = [];
changedFilesInsideScope = [];
logger.info("Running in AUDIT mode");
logger.info(`[audit] Watching file changes in: ${repositoryRoot}`);
logger.info(`[audit] Backfill will cache folder: ${outputFolder}`);
// Define globs
const ignoreGlobs = addGlobstars([
".git",
".cache",
logFolder,
internalCacheFolder
]);
const cacheFolderGlob = outputFolderAsArray(outputFolder).map(folder =>
path.posix.join("**", folder, "**")
);
watcher = chokidar
.watch(hashGlobs, {
ignored: ignoreGlobs,
cwd: repositoryRoot,
persistent: true,
ignoreInitial: true,
followSymlinks: false,
usePolling: true
})
.on("all", (event, filePath) => {
const logLine = `${filePath} (${event})`;
logger.silly(`[audit] File change: ${logLine}`);
return async (): Promise => {
const parsedBuildCommand = buildCommand.join(" ");
if (!parsedBuildCommand) {
throw new Error("Command not provided");
}
// Clear outputFolder to guarantee a deterministic cache
if (clearOutputFolder) {
await Promise.all(
outputFolderAsArray(outputFolder).map(folder => fs.remove(folder))
);
}
// Set up runner
logger.profile("buildCommand:run");
const runner = execa(parsedBuildCommand, {
shell: true,
...(process.env.NODE_ENV !== "test" ? { stdio: "inherit" } : {})
});
return (
runner
// Add build time to the performance logger
.then(() => {
logger.setTime("buildTime", "buildCommand:run");
})
const { npmPackageName, registryUrl, npmrcUserconfig } = this.options;
const temporaryNpmOutputFolder = path.join(
this.internalCacheFolder,
"npm",
hash,
"upload"
);
// Create package.json file
fs.outputJSONSync(path.join(temporaryNpmOutputFolder, "package.json"), {
name: npmPackageName,
version: `0.0.0-${hash}`
});
outputFolderAsArray(outputFolder).forEach(folder => {
fs.copySync(folder, path.join(temporaryNpmOutputFolder, folder));
});
// Upload package
try {
await execa(
"npm",
[
"publish",
"--registry",
registryUrl,
"--loglevel",
"error",
...(npmrcUserconfig ? ["--userconfig", npmrcUserconfig] : [])
],
{
protected async _put(
hash: string,
outputFolder: string | string[]
): Promise {
const localCacheFolder = this.getLocalCacheFolder(hash);
await Promise.all(
outputFolderAsArray(outputFolder).map(async folder => {
const outputFolderInCache = path.join(localCacheFolder, folder);
await fs.mkdirp(outputFolderInCache);
await fs.copy(folder, outputFolderInCache);
})
);
}
}
public async hashOfOutput(): Promise {
const outputFolderGlob = outputFolderAsArray(this.outputFolder).map(
p => `${p}/**`
);
return generateHashOfFiles(this.packageRoot, outputFolderGlob);
}
}
],
{ stdout: "inherit" }
);
} catch (error) {
fs.removeSync(temporaryNpmOutputFolder);
if (error.stderr.toString().indexOf("ETARGET") > -1) {
return false;
} else {
throw new Error(error);
}
}
}
await Promise.all(
outputFolderAsArray(outputFolder).map(async folder => {
const locationInCache = path.join(
packageFolderInTemporaryFolder,
folder
);
if (!fs.pathExistsSync(locationInCache)) {
return;
}
await fs.mkdirp(folder);
await fs.copy(path.join(locationInCache, folder), folder);
})
);
return true;
}
protected async _put(
hash: string,
outputFolder: string | string[]
): Promise {
const blobClient = createBlobClient(
this.options.connectionString,
this.options.container,
hash
);
const blockBlobClient = blobClient.getBlockBlobClient();
const outputFolders = outputFolderAsArray(outputFolder);
const tarStream = tar.create({ gzip: false }, outputFolders);
await blockBlobClient.uploadStream(
tarStream,
uploadOptions.bufferSize,
uploadOptions.maxBuffers
);
}
}
protected async _fetch(
hash: string,
outputFolder: string | string[]
): Promise {
const localCacheFolder = this.getLocalCacheFolder(hash);
if (!fs.pathExistsSync(localCacheFolder)) {
return false;
}
await Promise.all(
outputFolderAsArray(outputFolder).map(async folder => {
const locationInCache = path.join(localCacheFolder, folder);
if (!fs.pathExistsSync(locationInCache)) {
return;
}
await fs.mkdirp(folder);
await fs.copy(locationInCache, folder);
})
);
return true;
}
export async function main(): Promise {
try {
const config = createConfig();
const {
cacheStorageConfig,
clearOutputFolder,
hashGlobs,
internalCacheFolder,
logFolder,
logLevel,
outputFolder,
packageRoot
} = config;
if (logLevel) {
setLogLevel(logLevel);
}
const helpString = "Backfills unchanged packages.";
export async function generateHashOfFiles(
packageRoot: string,
glob?: string[]
): Promise {
if (!glob) {
glob = createConfig(packageRoot).hashGlobs;
}
const files = await fg(glob, {
cwd: packageRoot,
onlyFiles: false,
objectMode: true
});
files.sort((a, b) => a.path.localeCompare(b.path));
const hashes = await Promise.all(
files.map(async file => {
const hasher = crypto.createHash("sha1");
hasher.update(file.path);
if (!file.dirent.isDirectory()) {