Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
for (const pkgDepInfo of dependentPkgPaths) {
const pkgPath = pkgDepInfo.path;
const depHashFile = path.join(pkgPath, 'node_modules/.just', CacheFileName);
const depPackageJson = JSON.parse(fs.readFileSync(path.join(pkgPath, 'package.json'), 'utf-8'));
if (fs.existsSync(depHashFile)) {
const stat = fs.statSync(depHashFile);
timestampsByPackage[pkgDepInfo.name] = stat.mtimeMs;
} else if (depPackageJson.scripts) {
// always updated if no hash file is found for dependants
timestampsByPackage[pkgDepInfo.name] = new Date().getTime();
}
}
logger.perf('cache:getDependentHashTimestamps');
return timestampsByPackage;
}
function getDependentHashTimestamps() {
mark('cache:getDependentHashTimestamps');
const dependentPkgPaths = findDependents();
const timestampsByPackage: { [pkgName: string]: number } = {};
for (const pkgDepInfo of dependentPkgPaths) {
const pkgPath = pkgDepInfo.path;
const depHashFile = path.join(pkgPath, 'node_modules/.just', CacheFileName);
const depPackageJson = JSON.parse(fs.readFileSync(path.join(pkgPath, 'package.json'), 'utf-8'));
if (fs.existsSync(depHashFile)) {
const stat = fs.statSync(depHashFile);
timestampsByPackage[pkgDepInfo.name] = stat.mtimeMs;
} else if (depPackageJson.scripts) {
// always updated if no hash file is found for dependants
timestampsByPackage[pkgDepInfo.name] = new Date().getTime();
}
];
return deps
.map(dep => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const depPackageJson = resolveCwd(path.join(dep, 'package.json'))!;
if (!depPackageJson) {
return null;
}
return { name: dep, path: path.dirname(fs.realpathSync(depPackageJson)) };
})
.filter(p => p && p.path.indexOf('node_modules') === -1 && isChildOf(p.path, gitRoot)) as DepInfo[];
} catch (e) {
logger.error(`Invalid package.json detected at ${packageJsonFile} `, e);
return [];
}
}
export async function runGenerator(generator: any, args: any) {
const results = await generator.runActions(args, {
onComment: (comment: string) => {
logger.info(comment);
}
});
if (results.failures && results.failures.length > 0) {
throw new Error('Error: ' + results.failures[0].error);
}
// do something after the actions have run
for (let change of results.changes) {
if (change.path) {
logger.info(change.path);
}
}
}
const cachePath = getCachePath();
const cacheFile = path.join(cachePath, CacheFileName);
if (!fs.existsSync(cacheFile)) {
return false;
}
let shouldCache = false;
try {
const cachedHash = JSON.parse(fs.readFileSync(path.join(cachePath, CacheFileName)).toString());
// TODO: make a more robust comparison
shouldCache = JSON.stringify(currentHash) === JSON.stringify(cachedHash);
} catch (e) {
logger.warn('Invalid package-deps.json detected');
}
return shouldCache;
}
const packageRootPath = getPackageRootPath();
const packageDeps = getPackageDeps(packageRootPath);
const lockFileHashes = getLockFileHashes();
packageDeps.files = { ...packageDeps.files, ...lockFileHashes };
const hash = {
args,
taskName,
hash: packageDeps,
dependentHashTimestamps: getDependentHashTimestamps()
};
logger.perf('cache:getHash');
return hash;
}
function collectAllDependentPaths(pkgPath: string, collected: Set = new Set()) {
mark(`collectAllDependentPaths:${pkgPath}`);
const depPaths = getDepsPaths(pkgPath);
depPaths.forEach(depPath => collected.add(depPath));
for (const depPath of depPaths) {
if (!collected.has(depPath)) {
collectAllDependentPaths(depPath.path, collected);
}
}
logger.perf(`collectAllDependentPaths:${pkgPath}`);
return collected;
}
export function findDependents() {
mark('cache:findDependents');
const results = collectAllDependentPaths(findPackageRoot());
logger.perf('cache:findDependents');
return results;
}
export function findDependents() {
mark('cache:findDependents');
const results = collectAllDependentPaths(findPackageRoot());
logger.perf('cache:findDependents');
return results;
}
function collectAllDependentPaths(pkgPath: string, collected: Set = new Set()) {
mark(`collectAllDependentPaths:${pkgPath}`);
const depPaths = getDepsPaths(pkgPath);
depPaths.forEach(depPath => collected.add(depPath));
for (const depPath of depPaths) {
if (!collected.has(depPath)) {
collectAllDependentPaths(depPath.path, collected);
}
}
logger.perf(`collectAllDependentPaths:${pkgPath}`);
return collected;
}