diff --git a/change/change-615d3e38-16f6-4c1e-b301-ed36da8c458e.json b/change/change-615d3e38-16f6-4c1e-b301-ed36da8c458e.json new file mode 100644 index 000000000..61f5ab3db --- /dev/null +++ b/change/change-615d3e38-16f6-4c1e-b301-ed36da8c458e.json @@ -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" + } + ] +} \ No newline at end of file diff --git a/packages/just-scripts/src/tasks/cleanTask.ts b/packages/just-scripts/src/tasks/cleanTask.ts index bdf8c3d46..4a7d42be2 100644 --- a/packages/just-scripts/src/tasks/cleanTask.ts +++ b/packages/just-scripts/src/tasks/cleanTask.ts @@ -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 { @@ -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); diff --git a/packages/just-task/etc/just-task.api.md b/packages/just-task/etc/just-task.api.md index 116332466..b7e450dd5 100644 --- a/packages/just-task/etc/just-task.api.md +++ b/packages/just-task/etc/just-task.api.md @@ -27,7 +27,7 @@ export function chain(subjectTaskName: string): { after: (taskName: string) => void; }; -// @public (undocumented) +// @public @deprecated export function clearCache(): void; // @public (undocumented) @@ -116,7 +116,7 @@ export interface TaskContext { export interface TaskFunction extends TaskFunctionParams { // (undocumented) (this: TaskContext, done: (error?: any) => void): void | Duplex | NodeJS.Process | Promise | any; - // (undocumented) + // @deprecated (undocumented) cached?: () => void; // (undocumented) description?: string; diff --git a/packages/just-task/package.json b/packages/just-task/package.json index 242b29883..5479fd5c4 100644 --- a/packages/just-task/package.json +++ b/packages/just-task/package.json @@ -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", diff --git a/packages/just-task/src/cache.ts b/packages/just-task/src/cache.ts index 1d7e75834..3adcc2c6c 100644 --- a/packages/just-task/src/cache.ts +++ b/packages/just-task/src/cache.ts @@ -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; - 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 } diff --git a/packages/just-task/src/interfaces.ts b/packages/just-task/src/interfaces.ts index b0f61d73b..270f46660 100644 --- a/packages/just-task/src/interfaces.ts +++ b/packages/just-task/src/interfaces.ts @@ -12,6 +12,7 @@ export interface TaskContext { export interface TaskFunction extends TaskFunctionParams { (this: TaskContext, done: (error?: any) => void): void | Duplex | NodeJS.Process | Promise | any; + /** @deprecated Task caching has been removed. This property is a no-op. */ cached?: () => void; description?: string; } diff --git a/packages/just-task/src/package/findDependents.ts b/packages/just-task/src/package/findDependents.ts deleted file mode 100644 index daa28c371..000000000 --- a/packages/just-task/src/package/findDependents.ts +++ /dev/null @@ -1,68 +0,0 @@ -import * as fs from 'fs'; -import * as path from 'path'; -import { resolveCwd } from '../resolve'; -import { findPackageRoot } from './findPackageRoot'; -import { logger, mark } from '../logger'; -import { findGitRoot } from './findGitRoot'; -import { isChildOf } from '../paths'; - -interface DepInfo { - name: string; - path: string; -} - -export function findDependents(): Set { - mark('cache:findDependents'); - const results = collectAllDependentPaths(findPackageRoot()); - logger.perf('cache:findDependents'); - return results; -} - -function getDepsPaths(pkgPath: string): DepInfo[] { - const gitRoot = findGitRoot(); - const packageJsonFile = path.join(pkgPath, 'package.json'); - - try { - const packageJson = JSON.parse(fs.readFileSync(packageJsonFile).toString()); - - let deps: string[] = []; - deps = [ - ...deps, - ...(packageJson.dependencies ? Object.keys(packageJson.dependencies) : []), - ...(packageJson.devDependencies ? Object.keys(packageJson.devDependencies) : []), - ]; - - 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 []; - } -} - -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; -} diff --git a/packages/just-task/src/package/findGitRoot.ts b/packages/just-task/src/package/findGitRoot.ts deleted file mode 100644 index 26df38cea..000000000 --- a/packages/just-task/src/package/findGitRoot.ts +++ /dev/null @@ -1,28 +0,0 @@ -import * as path from 'path'; -import * as fs from 'fs-extra'; - -let gitRootCache = ''; - -export function findGitRoot(): string { - if (gitRootCache) { - return gitRootCache; - } - - let cwd = process.cwd(); - const root = path.parse(cwd).root; - let found = false; - while (!found && cwd !== root) { - if (fs.pathExistsSync(path.join(cwd, '.git'))) { - found = true; - break; - } - - cwd = path.dirname(cwd); - } - - if (found) { - gitRootCache = path.join(cwd); - } - - return gitRootCache; -} diff --git a/packages/just-task/src/package/findPackageRoot.ts b/packages/just-task/src/package/findPackageRoot.ts deleted file mode 100644 index b05d9a81a..000000000 --- a/packages/just-task/src/package/findPackageRoot.ts +++ /dev/null @@ -1,8 +0,0 @@ -import * as path from 'path'; -import { resolveCwd } from '../resolve'; - -export function findPackageRoot(): string { - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const packageJsonFilePath = resolveCwd('package.json')!; - return path.dirname(packageJsonFilePath); -} diff --git a/packages/just-task/src/paths.ts b/packages/just-task/src/paths.ts deleted file mode 100644 index 70ed52aff..000000000 --- a/packages/just-task/src/paths.ts +++ /dev/null @@ -1,6 +0,0 @@ -const path = require('path'); - -export function isChildOf(child: string, parent: string): boolean { - const relativePath = path.relative(child, parent); - return /^[.\/\\]+$/.test(relativePath); -} diff --git a/packages/just-task/src/task.ts b/packages/just-task/src/task.ts index 0d2296e44..200f01346 100644 --- a/packages/just-task/src/task.ts +++ b/packages/just-task/src/task.ts @@ -1,7 +1,6 @@ import { undertaker } from './undertaker'; import { wrapTask } from './wrapTask'; import { TaskFunction } from './interfaces'; -import { registerCachedTask } from './cache'; export function task( firstParam: string | TaskFunction, @@ -16,9 +15,6 @@ export function task( // task('default', 'build'); const wrapped = wrapTask(undertaker.series(secondParam)); - wrapped.cached = () => { - registerCachedTask(firstParam); - }; undertaker.task(firstParam, wrapped); @@ -27,9 +23,6 @@ export function task( // task('pretter', prettierTask()); // task('custom', () => { ... }); const wrapped = wrapTask(secondParam as TaskFunction) as TaskFunction; - wrapped.cached = () => { - registerCachedTask(firstParam); - }; undertaker.task(firstParam, wrapped); @@ -37,9 +30,6 @@ export function task( } else if (argCount === 3 && isString(firstParam) && isString(secondParam) && isTaskFunction(thirdParam)) { // task('custom', 'describes this thing', () => { ... }) const wrapped = wrapTask(thirdParam); - wrapped.cached = () => { - registerCachedTask(firstParam); - }; wrapped.description = secondParam; diff --git a/packages/just-task/src/undertaker.ts b/packages/just-task/src/undertaker.ts index b9c75c13a..2026ff3e5 100644 --- a/packages/just-task/src/undertaker.ts +++ b/packages/just-task/src/undertaker.ts @@ -2,7 +2,6 @@ import { logger } from './logger'; import chalk = require('chalk'); import { wrapTask } from './wrapTask'; import { Task } from './interfaces'; -import { clearCache } from './cache'; import Undertaker = require('undertaker'); const undertaker = new Undertaker(); @@ -84,8 +83,6 @@ undertaker.on('error', function (args: any) { logger.error(chalk.yellow('------------------------------------')); - clearCache(); - process.exitCode = 1; } else if (shouldLog(args)) { const duration = args.duration; diff --git a/yarn.lock b/yarn.lock index cd5c0aaaf..2dab3514d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -656,27 +656,6 @@ resolve "~1.22.1" semver "~7.5.4" -"@rushstack/node-core-library@5.20.3": - version "5.20.3" - resolved "https://registry.yarnpkg.com/@rushstack/node-core-library/-/node-core-library-5.20.3.tgz#42043fe42239fa0df906a03396cffde56021ce1f" - integrity sha512-95JgEPq2k7tHxhF9/OJnnyHDXfC9cLhhta0An/6MlkDsX2A6dTzDrTUG18vx4vjc280V0fi0xDH9iQczpSuWsw== - dependencies: - ajv "~8.18.0" - ajv-draft-04 "~1.0.0" - ajv-formats "~3.0.1" - fs-extra "~11.3.0" - import-lazy "~4.0.0" - jju "~1.4.0" - resolve "~1.22.1" - semver "~7.5.4" - -"@rushstack/package-deps-hash@^4.0.0": - version "4.7.7" - resolved "https://registry.yarnpkg.com/@rushstack/package-deps-hash/-/package-deps-hash-4.7.7.tgz#e33bead7a5e2d91b61abd3da068cf7e4d7ca1532" - integrity sha512-nj7wspFmch+SmPr6RT46zO9aKGD6IjVBP8z3JMmiiJq5p7WgNyqEENOBWGZ42Hwpav9qjj9hWxMggHJiwLg3mA== - dependencies: - "@rushstack/node-core-library" "5.20.3" - "@rushstack/rig-package@0.5.3": version "0.5.3" resolved "https://registry.yarnpkg.com/@rushstack/rig-package/-/rig-package-0.5.3.tgz#ea4d8a3458540b1295500149c04e645f23134e5d" @@ -1113,7 +1092,7 @@ ajv@^6.12.4: json-schema-traverse "^0.4.1" uri-js "^4.2.2" -ajv@^8.0.0, ajv@~8.18.0: +ajv@^8.0.0: version "8.18.0" resolved "https://registry.yarnpkg.com/ajv/-/ajv-8.18.0.tgz#8864186b6738d003eb3a933172bb3833e10cefbc" integrity sha512-PlXPeEWMXMZ7sPYOHqmDyCJzcfNrUr3fGNKtezX14ykXOEIvyK81d+qydx89KY5O71FKMPaQ2vBfBFI5NHR63A==