From a9232549477e07cb7a6aa1f55a33381fcdb53c7e Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Sat, 18 Apr 2020 14:11:09 +0200 Subject: [PATCH] packages/cli: moved build cache implementation to lib --- .../cli/src/commands/build-cache/index.ts | 43 +------------- packages/cli/src/commands/clean/clean.ts | 2 +- packages/cli/src/commands/plugin/build.ts | 6 +- .../build-cache => lib/buildCache}/archive.ts | 0 .../build-cache => lib/buildCache}/cache.ts | 0 packages/cli/src/lib/buildCache/index.ts | 18 ++++++ .../build-cache => lib/buildCache}/options.ts | 0 packages/cli/src/lib/buildCache/withCache.ts | 56 +++++++++++++++++++ 8 files changed, 79 insertions(+), 46 deletions(-) rename packages/cli/src/{commands/build-cache => lib/buildCache}/archive.ts (100%) rename packages/cli/src/{commands/build-cache => lib/buildCache}/cache.ts (100%) create mode 100644 packages/cli/src/lib/buildCache/index.ts rename packages/cli/src/{commands/build-cache => lib/buildCache}/options.ts (100%) create mode 100644 packages/cli/src/lib/buildCache/withCache.ts diff --git a/packages/cli/src/commands/build-cache/index.ts b/packages/cli/src/commands/build-cache/index.ts index a1007ca528..e58313c6b2 100644 --- a/packages/cli/src/commands/build-cache/index.ts +++ b/packages/cli/src/commands/build-cache/index.ts @@ -14,48 +14,9 @@ * limitations under the License. */ -import fs from 'fs-extra'; import { Command } from 'commander'; import { run } from 'lib/run'; -import { Cache } from './cache'; -import { parseOptions, Options } from './options'; - -function print(msg: string) { - process.stdout.write(`[build-cache] ${msg}\n`); -} - -// Wrap a build function with a cache, which won't call the build function on cache hit -export async function withCache( - options: Options, - buildFunc: () => Promise, -): Promise { - const key = await Cache.readInputKey(options.inputs); - if (!key) { - print('input directory is dirty, skipping cache'); - await fs.remove(options.output); - await buildFunc(); - return; - } - - const cache = await Cache.read(options); - - const cacheResult = cache.query(key); - if (cacheResult.hit) { - if (cacheResult.copy) { - print('external cache hit, copying archive to output folder'); - await cacheResult.copy(options.output); - } else { - print('cache hit, nothing to be done'); - } - return; - } - - print('cache miss, need to build'); - await fs.remove(options.output); - await buildFunc(); - - await cacheResult.archive(options.output, options.maxCacheEntries); -} +import { withCache, parseOptions } from 'lib/buildCache'; /* * The build-cache command is used to make builds a no-op if there are no changes to the package. @@ -69,5 +30,3 @@ export default async (cmd: Command, args: string[]) => { await run(args[0], args.slice(1)); }); }; - -export * from './options'; diff --git a/packages/cli/src/commands/clean/clean.ts b/packages/cli/src/commands/clean/clean.ts index f0dd179a98..06998a4ca3 100644 --- a/packages/cli/src/commands/clean/clean.ts +++ b/packages/cli/src/commands/clean/clean.ts @@ -16,8 +16,8 @@ import fs from 'fs-extra'; import { resolve as resolvePath, relative as relativePath } from 'path'; -import { getDefaultCacheOptions } from 'commands/build-cache/options'; import { paths } from 'lib/paths'; +import { getDefaultCacheOptions } from 'lib/buildCache'; export default async function clean() { const cacheOptions = getDefaultCacheOptions(); diff --git a/packages/cli/src/commands/plugin/build.ts b/packages/cli/src/commands/plugin/build.ts index 9be71c5e5b..aedf39e5b4 100644 --- a/packages/cli/src/commands/plugin/build.ts +++ b/packages/cli/src/commands/plugin/build.ts @@ -15,11 +15,11 @@ */ import { rollup, watch, OutputOptions } from 'rollup'; -import conf from './rollup.config'; import { Command } from 'commander'; -import { withCache, getDefaultCacheOptions } from 'commands/build-cache'; -import { paths } from 'helpers/paths'; import chalk from 'chalk'; +import { withCache, getDefaultCacheOptions } from 'lib/buildCache'; +import { paths } from 'lib/paths'; +import conf from './rollup.config'; function logError(error: any) { console.log(''); diff --git a/packages/cli/src/commands/build-cache/archive.ts b/packages/cli/src/lib/buildCache/archive.ts similarity index 100% rename from packages/cli/src/commands/build-cache/archive.ts rename to packages/cli/src/lib/buildCache/archive.ts diff --git a/packages/cli/src/commands/build-cache/cache.ts b/packages/cli/src/lib/buildCache/cache.ts similarity index 100% rename from packages/cli/src/commands/build-cache/cache.ts rename to packages/cli/src/lib/buildCache/cache.ts diff --git a/packages/cli/src/lib/buildCache/index.ts b/packages/cli/src/lib/buildCache/index.ts new file mode 100644 index 0000000000..70bcbd36b8 --- /dev/null +++ b/packages/cli/src/lib/buildCache/index.ts @@ -0,0 +1,18 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export * from './withCache'; +export * from './options'; diff --git a/packages/cli/src/commands/build-cache/options.ts b/packages/cli/src/lib/buildCache/options.ts similarity index 100% rename from packages/cli/src/commands/build-cache/options.ts rename to packages/cli/src/lib/buildCache/options.ts diff --git a/packages/cli/src/lib/buildCache/withCache.ts b/packages/cli/src/lib/buildCache/withCache.ts new file mode 100644 index 0000000000..d21de94865 --- /dev/null +++ b/packages/cli/src/lib/buildCache/withCache.ts @@ -0,0 +1,56 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import fs from 'fs-extra'; +import { Cache } from './cache'; +import { Options } from './options'; + +function print(msg: string) { + process.stdout.write(`[build-cache] ${msg}\n`); +} + +// Wrap a build function with a cache, which won't call the build function on cache hit +export async function withCache( + options: Options, + buildFunc: () => Promise, +): Promise { + const key = await Cache.readInputKey(options.inputs); + if (!key) { + print('input directory is dirty, skipping cache'); + await fs.remove(options.output); + await buildFunc(); + return; + } + + const cache = await Cache.read(options); + + const cacheResult = cache.query(key); + if (cacheResult.hit) { + if (cacheResult.copy) { + print('external cache hit, copying archive to output folder'); + await cacheResult.copy(options.output); + } else { + print('cache hit, nothing to be done'); + } + return; + } + + print('cache miss, need to build'); + await fs.remove(options.output); + await buildFunc(); + + await cacheResult.archive(options.output, options.maxCacheEntries); +}