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
18 changes: 18 additions & 0 deletions change/change-615d3e38-16f6-4c1e-b301-ed36da8c458e.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"changes": [
{
"type": "patch",
"comment": "Remove no-op clearCache from cleanTask",
"packageName": "just-scripts",
"email": "elcraig@microsoft.com",
"dependentChangeType": "patch"
},
{
"type": "minor",
"comment": "Remove remains of caching code that was no longer being used (APIs are kept but deprecated until next major version)",
"packageName": "just-task",
"email": "elcraig@microsoft.com",
"dependentChangeType": "patch"
}
]
}
19 changes: 7 additions & 12 deletions packages/just-scripts/src/tasks/cleanTask.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as fse from 'fs-extra';
import * as path from 'path';
import { logger, TaskFunction, clearCache } from 'just-task';
import { logger, TaskFunction } from 'just-task';
import parallelLimit = require('run-parallel-limit');

export interface CleanTaskOptions {
Expand Down Expand Up @@ -36,17 +36,12 @@ export function cleanTask(pathsOrOptions: string[] | CleanTaskOptions = {}, limi
return function clean(done: (err: Error | null) => void) {
logger.info(`Removing [${paths.map(p => path.relative(process.cwd(), p)).join(', ')}]`);

const cleanTasks = paths
.map(
cleanPath =>
function (cb: (error: Error | null) => void) {
fse.remove(cleanPath, cb);
},
)
.concat((cb: (error: Error | null) => void) => {
clearCache();
cb(null);
});
const cleanTasks = paths.map(
cleanPath =>
function (cb: (error: Error | null) => void) {
fse.remove(cleanPath, cb);
},
);

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
parallelLimit(cleanTasks, limit!, done);
Expand Down
4 changes: 2 additions & 2 deletions packages/just-task/etc/just-task.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function chain(subjectTaskName: string): {
after: (taskName: string) => void;
};

// @public (undocumented)
// @public @deprecated
export function clearCache(): void;

// @public (undocumented)
Expand Down Expand Up @@ -116,7 +116,7 @@ export interface TaskContext {
export interface TaskFunction extends TaskFunctionParams {
// (undocumented)
(this: TaskContext, done: (error?: any) => void): void | Duplex | NodeJS.Process | Promise<never> | any;
// (undocumented)
// @deprecated (undocumented)
cached?: () => void;
// (undocumented)
description?: string;
Expand Down
1 change: 0 additions & 1 deletion packages/just-task/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"node": ">=14"
},
"dependencies": {
"@rushstack/package-deps-hash": "^4.0.0",
"@types/chokidar": "^2.1.3",
"@types/undertaker": "^1.2.8",
"@types/yargs-parser": "^20.2.2",
Expand Down
173 changes: 5 additions & 168 deletions packages/just-task/src/cache.ts
Original file line number Diff line number Diff line change
@@ -1,170 +1,7 @@
import { getPackageDeps } from '@rushstack/package-deps-hash';
import { argv } from './option';
import { resolveCwd } from './resolve';
import * as fs from 'fs-extra';
import * as path from 'path';
import { logger, mark } from './logger';
import { findDependents } from './package/findDependents';
import { findGitRoot } from './package/findGitRoot';
import { spawnSync } from 'child_process';

const cachedTask: string[] = [];
const CacheFileName = 'package-deps.json';

export function registerCachedTask(taskName: string): void {
cachedTask.push(taskName);
}

/**
* Clears the task cache.
* @deprecated Task caching has been removed. This function is a no-op.
*/
export function clearCache(): void {
const cachePath = getCachePath();
const cacheFile = path.join(cachePath, CacheFileName);

if (fs.existsSync(cacheFile)) {
fs.removeSync(cacheFile);
}
}

export function isCached(taskName: string): boolean {
if (cachedTask.indexOf(taskName) < 0) {
return false;
}

const currentHash = getHash(taskName);
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;
}

export function saveCache(taskName: string): void {
if (cachedTask.indexOf(taskName) < 0) {
return;
}

const cachePath = getCachePath();

if (!fs.pathExistsSync(cachePath)) {
fs.mkdirpSync(cachePath);
}

const cacheHash = getHash(taskName);

if (cacheHash) {
fs.writeFileSync(path.join(cachePath, 'package-deps.json'), JSON.stringify(cacheHash, null, 2));
}
}

function getPackageRootPath() {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const packageJsonFilePath = resolveCwd('package.json')!;
return path.dirname(packageJsonFilePath);
}

function getCachePath() {
const rootPath = getPackageRootPath();
return path.join(rootPath, 'node_modules/.just');
}

interface CacheHash {
args: { [arg: string]: string };
taskName: string;
hash: Record<string, string>;
dependentHashTimestamps: { [pkgName: string]: number };
}

function getLockFileHashes(): { [file: string]: string } {
const results: { [file: string]: string } = {};

const lockFiles = ['shrinkwrap.yml', 'package-lock.json', 'yarn.lock', 'pnpmfile.js'];
const gitRoot = findGitRoot();
const lsFileResults = spawnSync('git', ['ls-files', ...lockFiles], { cwd: gitRoot });
if (lsFileResults.status !== 0) {
return {};
}

const foundLockFiles = lsFileResults.stdout
.toString()
.split(/\n/)
.map(l => l.trim());

const hashResults = spawnSync('git', ['hash-object', ...foundLockFiles], { cwd: gitRoot });

if (hashResults.status !== 0) {
return {};
}

const hashes = hashResults.stdout
.toString()
.split(/\n/)
.map(l => l.trim());

foundLockFiles.forEach((foundLockFile, index) => {
results[foundLockFile] = hashes[index];
});

return results;
}

function getHash(taskName: string): CacheHash | null {
mark('cache:getHash');

const { ...args } = argv();

const packageRootPath = getPackageRootPath();

const packageDeps = {
...Object.fromEntries(getPackageDeps(packageRootPath)),
...getLockFileHashes(),
};

const hash = {
args,
taskName,
hash: packageDeps,
dependentHashTimestamps: getDependentHashTimestamps(),
};

logger.perf('cache:getHash');

return hash;
}

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();
}
}

logger.perf('cache:getDependentHashTimestamps');

return timestampsByPackage;
// no-op
}
1 change: 1 addition & 0 deletions packages/just-task/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface TaskContext {

export interface TaskFunction extends TaskFunctionParams {
(this: TaskContext, done: (error?: any) => void): void | Duplex | NodeJS.Process | Promise<never> | any;
/** @deprecated Task caching has been removed. This property is a no-op. */
cached?: () => void;
description?: string;
}
68 changes: 0 additions & 68 deletions packages/just-task/src/package/findDependents.ts

This file was deleted.

28 changes: 0 additions & 28 deletions packages/just-task/src/package/findGitRoot.ts

This file was deleted.

8 changes: 0 additions & 8 deletions packages/just-task/src/package/findPackageRoot.ts

This file was deleted.

6 changes: 0 additions & 6 deletions packages/just-task/src/paths.ts

This file was deleted.

Loading
Loading