Skip to content

Commit bae761b

Browse files
authored
Scope cache (#2784)
- add an option to not use cache when loading scope - do not use scope cache by default when loading scope programmatically
1 parent 02c8a1c commit bae761b

File tree

6 files changed

+61
-31
lines changed

6 files changed

+61
-31
lines changed

CHANGELOG.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,25 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [unreleased]
99

10+
- add an option to not use cache when loading scope
11+
- do not use scope cache by default when loading scope programmatically
12+
1013
## [[14.8.3] - 2020-07-01](https://github.com/teambit/bit/releases/tag/v14.8.3)
1114

15+
### Bug Fixes
16+
1217
- [#2780](https://github.com/teambit/bit/issues/2780) - fix dists codemod of changing one scope to another to not be triggered without --rewire flag
1318
- add timeout option for load core extension via api
14-
- wait for harmony to load if you load it many times in parallel via the api
1519

16-
## [[14.8.3-dev.1] - 2020-06-29]
20+
### Internal
1721

22+
- wait for harmony to load if you load it many times in parallel via the api
1823
- expose extensions declarations and instances from api
1924

2025
## [[14.8.2] - 2020-06-29](https://github.com/teambit/bit/releases/tag/v14.8.2)
2126

27+
### Bug Fixes
28+
2229
- do not show loader for internal commands
2330
- fix error when trying to load extension in a folder which is not a workspace or scope
2431

src/api.ts

+19-7
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,28 @@ type LoadCoreExtensionsOptions = {
2222
};
2323

2424
export function show(scopePath: string, id: string, opts?: Record<string, any>) {
25+
// When using the API programmatically do not use the scope cache by default
26+
const loadScopeFromCache = opts && opts.loadScopeFromCache !== undefined ? !!opts.loadScopeFromCache : false;
2527
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
26-
return getScopeComponent({ scopePath, id, allVersions: opts && opts.versions }).then(({ component }) => {
27-
if (Array.isArray(component)) {
28-
return component.map(v => v.toObject());
28+
return getScopeComponent({ scopePath, id, allVersions: opts && opts.versions, loadScopeFromCache }).then(
29+
({ component }) => {
30+
if (Array.isArray(component)) {
31+
return component.map(v => v.toObject());
32+
}
33+
return component.toObject();
2934
}
30-
return component.toObject();
31-
});
35+
);
3236
}
33-
export function list(scopePath: string) {
34-
return scopeList(scopePath).then(listScopeResult => listScopeResult.map(result => result.id.toString()));
37+
export function list(
38+
scopePath: string,
39+
namespacesUsingWildcards?: string,
40+
opts: { loadScopeFromCache?: boolean } = {}
41+
) {
42+
// When using the API programmatically do not use the scope cache by default
43+
const loadScopeFromCache = opts && opts.loadScopeFromCache !== undefined ? !!opts.loadScopeFromCache : false;
44+
return scopeList(scopePath, namespacesUsingWildcards, loadScopeFromCache).then(listScopeResult =>
45+
listScopeResult.map(result => result.id.toString())
46+
);
3547
}
3648

3749
export async function buildOne(

src/api/consumer/lib/get-scope-component.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,21 @@ export default (async function getScopeComponent({
1313
allVersions,
1414
scopePath,
1515
showDependents,
16-
showDependencies
16+
showDependencies,
17+
loadScopeFromCache
1718
}: {
1819
id: string;
1920
allVersions: boolean | null | undefined;
2021
scopePath: string | null | undefined; // used by the api (see /src/api.js)
2122
showDependents: boolean;
2223
showDependencies: boolean;
24+
loadScopeFromCache: boolean;
2325
}): Promise<{ component: Component[] | Component }> {
2426
const bitId: BitId = BitId.parse(id, true); // user used --remote so we know it has a scope
2527

2628
if (scopePath) {
2729
// coming from the api
28-
const scope: Scope = await loadScope(scopePath);
30+
const scope: Scope = await loadScope(scopePath, loadScopeFromCache);
2931
const component = await showComponentUsingScope(scope);
3032
return { component };
3133
}

src/api/scope/lib/scope-list.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ import { loadScope } from '../../../scope';
22
import ComponentsList from '../../../consumer/component/components-list';
33
import { ListScopeResult } from '../../../consumer/component/components-list';
44

5-
export default function list(path: string, namespacesUsingWildcards?: string): Promise<ListScopeResult[]> {
6-
return loadScope(path).then(scope => ComponentsList.listLocalScope(scope, namespacesUsingWildcards));
5+
export default function list(
6+
path: string,
7+
namespacesUsingWildcards?: string,
8+
loadScopeFromCache = true
9+
): Promise<ListScopeResult[]> {
10+
return loadScope(path, loadScopeFromCache).then(scope =>
11+
ComponentsList.listLocalScope(scope, namespacesUsingWildcards)
12+
);
713
}

src/scope/scope-loader.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,22 @@ import Scope from './scope';
33
import { resolveHomePath } from '../utils';
44
import { ScopeNotFound } from './exceptions';
55

6-
export default function loadScope(currentPath?: string | null | undefined): Promise<Scope> {
6+
export default function loadScope(currentPath?: string | null | undefined, useCache = true): Promise<Scope> {
77
if (!currentPath) currentPath = process.cwd();
88
try {
9-
return Scope.load(path.resolve(resolveHomePath(currentPath)));
9+
return Scope.load(path.resolve(resolveHomePath(currentPath)), useCache);
1010
} catch (err) {
1111
return Promise.reject(err);
1212
}
1313
}
1414

1515
export async function loadScopeIfExist(
1616
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
17-
currentPath?: string | null | undefined = process.cwd()
17+
currentPath?: string | null | undefined = process.cwd(),
18+
useCache = true
1819
): Promise<Scope | undefined> {
1920
try {
20-
return await loadScope(currentPath);
21+
return await loadScope(currentPath, useCache);
2122
} catch (err) {
2223
if (err instanceof ScopeNotFound) {
2324
return undefined;

src/scope/scope.ts

+16-14
Original file line numberDiff line numberDiff line change
@@ -790,25 +790,27 @@ export default class Scope {
790790
Scope.scopeCache = {};
791791
}
792792

793-
static async load(absPath: string): Promise<Scope> {
793+
static async load(absPath: string, useCache = true): Promise<Scope> {
794794
let scopePath = propogateUntil(absPath);
795795
if (!scopePath) throw new ScopeNotFound(absPath);
796796
if (fs.existsSync(pathLib.join(scopePath, BIT_HIDDEN_DIR))) {
797797
scopePath = pathLib.join(scopePath, BIT_HIDDEN_DIR);
798798
}
799-
if (!Scope.scopeCache[scopePath]) {
800-
const scopeJsonPath = getScopeJsonPath(scopePath);
801-
const scopeJsonExist = fs.existsSync(scopeJsonPath);
802-
let scopeJson;
803-
if (scopeJsonExist) {
804-
scopeJson = await ScopeJson.loadFromFile(scopeJsonPath);
805-
} else {
806-
scopeJson = Scope.ensureScopeJson(scopePath);
807-
}
808-
const objects = await Repository.load({ scopePath, scopeJson });
809-
const scope = new Scope({ path: scopePath, scopeJson, objects });
810-
Scope.scopeCache[scopePath] = scope;
799+
if (useCache && Scope.scopeCache[scopePath]) {
800+
logger.info(`taking scope at ${scopePath} from cache`);
801+
return Scope.scopeCache[scopePath];
802+
}
803+
const scopeJsonPath = getScopeJsonPath(scopePath);
804+
const scopeJsonExist = fs.existsSync(scopeJsonPath);
805+
let scopeJson;
806+
if (scopeJsonExist) {
807+
scopeJson = await ScopeJson.loadFromFile(scopeJsonPath);
808+
} else {
809+
scopeJson = Scope.ensureScopeJson(scopePath);
811810
}
812-
return Scope.scopeCache[scopePath];
811+
const objects = await Repository.load({ scopePath, scopeJson });
812+
const scope = new Scope({ path: scopePath, scopeJson, objects });
813+
Scope.scopeCache[scopePath] = scope;
814+
return scope;
813815
}
814816
}

0 commit comments

Comments
 (0)