@teambit/workspace.modules.node-modules-linker 0.0.292 → 0.0.296
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/codemod-components.ts
CHANGED
|
@@ -74,7 +74,6 @@ async function codemodComponent(
|
|
|
74
74
|
pathNormalizeToLinux(file.relative)
|
|
75
75
|
];
|
|
76
76
|
if (!relativeInstances) return;
|
|
77
|
-
// @ts-ignore
|
|
78
77
|
const fileBefore = file.contents.toString() as string;
|
|
79
78
|
let newFileString = fileBefore;
|
|
80
79
|
await Promise.all(
|
|
@@ -93,7 +92,6 @@ async function codemodComponent(
|
|
|
93
92
|
})
|
|
94
93
|
);
|
|
95
94
|
if (fileBefore !== newFileString) {
|
|
96
|
-
// @ts-ignore
|
|
97
95
|
file.contents = Buffer.from(newFileString);
|
|
98
96
|
files.push(file);
|
|
99
97
|
}
|
|
@@ -93,7 +93,6 @@ async function codemodComponent(workspace, component) {
|
|
|
93
93
|
await Promise.all(component.filesystem.files.map(async file => {
|
|
94
94
|
const relativeInstances = issues.getIssue(_componentIssues().IssuesClasses.RelativeComponentsAuthored)?.data[(0, _legacy().pathNormalizeToLinux)(file.relative)];
|
|
95
95
|
if (!relativeInstances) return;
|
|
96
|
-
// @ts-ignore
|
|
97
96
|
const fileBefore = file.contents.toString();
|
|
98
97
|
let newFileString = fileBefore;
|
|
99
98
|
await Promise.all(relativeInstances.map(async relativeEntry => {
|
|
@@ -113,7 +112,6 @@ async function codemodComponent(workspace, component) {
|
|
|
113
112
|
newFileString = (0, _legacy().replacePackageName)(newFileString, stringToReplace, packageNameSupportCss);
|
|
114
113
|
}));
|
|
115
114
|
if (fileBefore !== newFileString) {
|
|
116
|
-
// @ts-ignore
|
|
117
115
|
file.contents = Buffer.from(newFileString);
|
|
118
116
|
files.push(file);
|
|
119
117
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_path","data","_interopRequireDefault","require","_componentIssues","_componentId","_legacy","_pkgModules","_component","e","__esModule","default","changeCodeFromRelativeToModulePaths","workspace","bitIds","components","loadComponents","componentsWithRelativeIssues","filter","c","state","issues","getIssue","IssuesClasses","RelativeComponentsAuthored","dataToPersist","DataToPersist","codemodResults","Promise","all","map","component","files","warnings","codemodComponent","addManyFiles","id","changedFiles","f","relative","persistAllToFS","idsToReload","length","reloadComponents","compIds","clearAllComponentsCache","failedComps","toString","join","Error","componentsIds","ComponentIdList","fromArray","listIds","getMany","filesystem","file","relativeInstances","pathNormalizeToLinux","fileBefore","contents","newFileString","relativeEntry","componentId","requiredComponent","get","consumerComp","_consumer","packageName","componentIdToPackageName","cssFamily","isCss","includes","extname","packageNameSupportCss","stringToReplace","getNameWithoutInternalPath","replacePackageName","Buffer","from","push","importSource","componentMap","consumer","bitMap","getComponentIfExist","rootDir","mainFile","pathJoinLinux","filePathRelativeToWorkspace","relativePath","sourceRelativePath","internalPath","pathRelativeLinux","removeLastOccurrence","str","toRemove","replace","RegExp","endsWith","internalPathNoExt","path","internalPathNoIndex"],"sources":["codemod-components.ts"],"sourcesContent":["import path from 'path';\nimport type { Workspace } from '@teambit/workspace';\nimport type { RelativeComponentsAuthoredEntry } from '@teambit/component-issues';\nimport { IssuesClasses } from '@teambit/component-issues';\nimport type { Component } from '@teambit/component';\nimport type { ComponentID } from '@teambit/component-id';\nimport { ComponentIdList } from '@teambit/component-id';\nimport { pathJoinLinux, pathNormalizeToLinux, pathRelativeLinux, replacePackageName } from '@teambit/legacy.utils';\nimport { componentIdToPackageName } from '@teambit/pkg.modules.component-package-name';\nimport type { SourceFile } from '@teambit/component.sources';\nimport { DataToPersist } from '@teambit/component.sources';\nimport type { ConsumerComponent } from '@teambit/legacy.consumer-component';\n\nexport type CodemodResult = {\n id: ComponentID;\n changedFiles: string[];\n warnings?: string[];\n};\n\nexport async function changeCodeFromRelativeToModulePaths(\n workspace: Workspace,\n bitIds: ComponentID[]\n): Promise<CodemodResult[]> {\n const components = await loadComponents(workspace, bitIds);\n const componentsWithRelativeIssues = components.filter(\n (c) => c.state.issues && c.state.issues.getIssue(IssuesClasses.RelativeComponentsAuthored)\n );\n const dataToPersist = new DataToPersist();\n const codemodResults = await Promise.all(\n componentsWithRelativeIssues.map(async (component) => {\n const { files, warnings } = await codemodComponent(workspace, component);\n dataToPersist.addManyFiles(files);\n return { id: component.id, changedFiles: files.map((f) => f.relative), warnings };\n })\n );\n await dataToPersist.persistAllToFS();\n const idsToReload = codemodResults.filter((c) => !c.warnings || c.warnings.length === 0).map((c) => c.id);\n await reloadComponents(workspace, idsToReload);\n\n return codemodResults.filter((c) => c.changedFiles.length || c.warnings);\n}\n\nasync function reloadComponents(workspace: Workspace, compIds: ComponentID[]) {\n workspace.clearAllComponentsCache();\n if (!compIds.length) return;\n const components = await loadComponents(workspace, compIds);\n const componentsWithRelativeIssues = components.filter(\n (c) => c.state.issues && c.state.issues.getIssue(IssuesClasses.RelativeComponentsAuthored)\n );\n if (componentsWithRelativeIssues.length) {\n const failedComps = componentsWithRelativeIssues.map((c) => c.id.toString()).join(', ');\n throw new Error(`failed rewiring the following components: ${failedComps}`);\n }\n}\n\nasync function loadComponents(workspace: Workspace, bitIds: ComponentID[]): Promise<Component[]> {\n const componentsIds = bitIds.length ? ComponentIdList.fromArray(bitIds) : await workspace.listIds();\n const components = await workspace.getMany(componentsIds);\n\n return components;\n}\n\nasync function codemodComponent(\n workspace: Workspace,\n component: Component\n): Promise<{ files: SourceFile[]; warnings?: string[] }> {\n const issues = component.state.issues;\n const files: SourceFile[] = [];\n if (!issues || !issues.getIssue(IssuesClasses.RelativeComponentsAuthored)) return { files };\n const warnings: string[] = [];\n await Promise.all(\n component.filesystem.files.map(async (file: SourceFile) => {\n const relativeInstances = issues.getIssue(IssuesClasses.RelativeComponentsAuthored)?.data[\n pathNormalizeToLinux(file.relative)\n ];\n if (!relativeInstances) return;\n // @ts-ignore\n const fileBefore = file.contents.toString() as string;\n let newFileString = fileBefore;\n await Promise.all(\n relativeInstances.map(async (relativeEntry: RelativeComponentsAuthoredEntry) => {\n const id = relativeEntry.componentId;\n const requiredComponent = await workspace.get(id);\n const consumerComp = requiredComponent.state._consumer as ConsumerComponent;\n const packageName = componentIdToPackageName({ ...consumerComp, id });\n const cssFamily = ['.css', '.scss', '.less', '.sass'];\n const isCss = cssFamily.includes(file.extname);\n const packageNameSupportCss = isCss ? `~${packageName}` : packageName;\n const stringToReplace = getNameWithoutInternalPath(workspace, relativeEntry);\n // @todo: the \"dist\" should be replaced by the compiler dist-dir.\n // newFileString = replacePackageName(newFileString, stringToReplace, packageNameSupportCss, 'dist');\n newFileString = replacePackageName(newFileString, stringToReplace, packageNameSupportCss);\n })\n );\n if (fileBefore !== newFileString) {\n // @ts-ignore\n file.contents = Buffer.from(newFileString);\n files.push(file);\n }\n })\n );\n return { files, warnings };\n}\n\n/**\n * e.g.\n * importSource: '../workspace/workspace.ui'\n * sourceRelativePath: 'extensions/workspace/workspace.ui.tsx'\n * rootDir in .bitmap: 'extensions/workspace'.\n *\n * expected to return \"../workspace\", as this is the path to the package root without the internal path.\n *\n * eventually, only this string is replaced by the new package-name and the internal-path part\n * remains intact. ('../workspace/workspace.ui' => '@bit/workspace/workspace.ui').\n */\nfunction getNameWithoutInternalPath(workspace: Workspace, relativeEntry: RelativeComponentsAuthoredEntry): string {\n const importSource = relativeEntry.importSource;\n const componentMap = workspace.consumer.bitMap.getComponentIfExist(relativeEntry.componentId);\n if (!componentMap) return importSource;\n const rootDir = componentMap.rootDir;\n if (!rootDir) return importSource;\n const mainFile = pathJoinLinux(rootDir, componentMap.mainFile);\n const filePathRelativeToWorkspace = relativeEntry.relativePath.sourceRelativePath;\n if (filePathRelativeToWorkspace === mainFile) {\n return importSource;\n }\n // the importSource is not the main-file but an internal file, remove the internal part.\n const internalPath = pathRelativeLinux(rootDir, filePathRelativeToWorkspace);\n const removeLastOccurrence = (str, toRemove) => str.replace(new RegExp(`/${toRemove}$`), '');\n if (importSource.endsWith(internalPath)) {\n return removeLastOccurrence(importSource, internalPath);\n }\n const internalPathNoExt = internalPath.replace(path.extname(internalPath), '');\n if (importSource.endsWith(internalPathNoExt)) {\n return removeLastOccurrence(importSource, internalPathNoExt);\n }\n const internalPathNoIndex = removeLastOccurrence(internalPathNoExt, 'index');\n if (importSource.endsWith(internalPathNoIndex)) {\n return removeLastOccurrence(importSource, internalPathNoIndex);\n }\n\n // unable to find anything useful. just return the importSource.\n return importSource;\n}\n"],"mappings":";;;;;;AAAA,SAAAA,MAAA;EAAA,MAAAC,IAAA,GAAAC,sBAAA,CAAAC,OAAA;EAAAH,KAAA,YAAAA,CAAA;IAAA,OAAAC,IAAA;EAAA;EAAA,OAAAA,IAAA;AAAA;AAGA,SAAAG,iBAAA;EAAA,MAAAH,IAAA,GAAAE,OAAA;EAAAC,gBAAA,YAAAA,CAAA;IAAA,OAAAH,IAAA;EAAA;EAAA,OAAAA,IAAA;AAAA;AAGA,SAAAI,aAAA;EAAA,MAAAJ,IAAA,GAAAE,OAAA;EAAAE,YAAA,YAAAA,CAAA;IAAA,OAAAJ,IAAA;EAAA;EAAA,OAAAA,IAAA;AAAA;AACA,SAAAK,QAAA;EAAA,MAAAL,IAAA,GAAAE,OAAA;EAAAG,OAAA,YAAAA,CAAA;IAAA,OAAAL,IAAA;EAAA;EAAA,OAAAA,IAAA;AAAA;AACA,SAAAM,YAAA;EAAA,MAAAN,IAAA,GAAAE,OAAA;EAAAI,WAAA,YAAAA,CAAA;IAAA,OAAAN,IAAA;EAAA;EAAA,OAAAA,IAAA;AAAA;AAEA,SAAAO,WAAA;EAAA,MAAAP,IAAA,GAAAE,OAAA;EAAAK,UAAA,YAAAA,CAAA;IAAA,OAAAP,IAAA;EAAA;EAAA,OAAAA,IAAA;AAAA;AAA2D,SAAAC,uBAAAO,CAAA,WAAAA,CAAA,IAAAA,CAAA,CAAAC,UAAA,GAAAD,CAAA,KAAAE,OAAA,EAAAF,CAAA;AASpD,eAAeG,mCAAmCA,CACvDC,SAAoB,EACpBC,MAAqB,E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AO,CAACmB,YAAY,CAAC,EAAE,EAAE,CAAC;EAC9E,IAAIX,YAAY,CAACkB,QAAQ,CAACC,iBAAiB,CAAC,EAAE;IAC5C,OAAON,oBAAoB,CAACb,YAAY,EAAEmB,iBAAiB,CAAC;EAC9D;EACA,MAAME,mBAAmB,GAAGR,oBAAoB,CAACM,iBAAiB,EAAE,OAAO,CAAC;EAC5E,IAAInB,YAAY,CAACkB,QAAQ,CAACG,mBAAmB,CAAC,EAAE;IAC9C,OAAOR,oBAAoB,CAACb,YAAY,EAAEqB,mBAAmB,CAAC;EAChE;;EAEA;EACA,OAAOrB,YAAY;AACrB","ignoreList":[]}
|
|
1
|
+
{"version":3,"names":["_path","data","_interopRequireDefault","require","_componentIssues","_componentId","_legacy","_pkgModules","_component","e","__esModule","default","changeCodeFromRelativeToModulePaths","workspace","bitIds","components","loadComponents","componentsWithRelativeIssues","filter","c","state","issues","getIssue","IssuesClasses","RelativeComponentsAuthored","dataToPersist","DataToPersist","codemodResults","Promise","all","map","component","files","warnings","codemodComponent","addManyFiles","id","changedFiles","f","relative","persistAllToFS","idsToReload","length","reloadComponents","compIds","clearAllComponentsCache","failedComps","toString","join","Error","componentsIds","ComponentIdList","fromArray","listIds","getMany","filesystem","file","relativeInstances","pathNormalizeToLinux","fileBefore","contents","newFileString","relativeEntry","componentId","requiredComponent","get","consumerComp","_consumer","packageName","componentIdToPackageName","cssFamily","isCss","includes","extname","packageNameSupportCss","stringToReplace","getNameWithoutInternalPath","replacePackageName","Buffer","from","push","importSource","componentMap","consumer","bitMap","getComponentIfExist","rootDir","mainFile","pathJoinLinux","filePathRelativeToWorkspace","relativePath","sourceRelativePath","internalPath","pathRelativeLinux","removeLastOccurrence","str","toRemove","replace","RegExp","endsWith","internalPathNoExt","path","internalPathNoIndex"],"sources":["codemod-components.ts"],"sourcesContent":["import path from 'path';\nimport type { Workspace } from '@teambit/workspace';\nimport type { RelativeComponentsAuthoredEntry } from '@teambit/component-issues';\nimport { IssuesClasses } from '@teambit/component-issues';\nimport type { Component } from '@teambit/component';\nimport type { ComponentID } from '@teambit/component-id';\nimport { ComponentIdList } from '@teambit/component-id';\nimport { pathJoinLinux, pathNormalizeToLinux, pathRelativeLinux, replacePackageName } from '@teambit/legacy.utils';\nimport { componentIdToPackageName } from '@teambit/pkg.modules.component-package-name';\nimport type { SourceFile } from '@teambit/component.sources';\nimport { DataToPersist } from '@teambit/component.sources';\nimport type { ConsumerComponent } from '@teambit/legacy.consumer-component';\n\nexport type CodemodResult = {\n id: ComponentID;\n changedFiles: string[];\n warnings?: string[];\n};\n\nexport async function changeCodeFromRelativeToModulePaths(\n workspace: Workspace,\n bitIds: ComponentID[]\n): Promise<CodemodResult[]> {\n const components = await loadComponents(workspace, bitIds);\n const componentsWithRelativeIssues = components.filter(\n (c) => c.state.issues && c.state.issues.getIssue(IssuesClasses.RelativeComponentsAuthored)\n );\n const dataToPersist = new DataToPersist();\n const codemodResults = await Promise.all(\n componentsWithRelativeIssues.map(async (component) => {\n const { files, warnings } = await codemodComponent(workspace, component);\n dataToPersist.addManyFiles(files);\n return { id: component.id, changedFiles: files.map((f) => f.relative), warnings };\n })\n );\n await dataToPersist.persistAllToFS();\n const idsToReload = codemodResults.filter((c) => !c.warnings || c.warnings.length === 0).map((c) => c.id);\n await reloadComponents(workspace, idsToReload);\n\n return codemodResults.filter((c) => c.changedFiles.length || c.warnings);\n}\n\nasync function reloadComponents(workspace: Workspace, compIds: ComponentID[]) {\n workspace.clearAllComponentsCache();\n if (!compIds.length) return;\n const components = await loadComponents(workspace, compIds);\n const componentsWithRelativeIssues = components.filter(\n (c) => c.state.issues && c.state.issues.getIssue(IssuesClasses.RelativeComponentsAuthored)\n );\n if (componentsWithRelativeIssues.length) {\n const failedComps = componentsWithRelativeIssues.map((c) => c.id.toString()).join(', ');\n throw new Error(`failed rewiring the following components: ${failedComps}`);\n }\n}\n\nasync function loadComponents(workspace: Workspace, bitIds: ComponentID[]): Promise<Component[]> {\n const componentsIds = bitIds.length ? ComponentIdList.fromArray(bitIds) : await workspace.listIds();\n const components = await workspace.getMany(componentsIds);\n\n return components;\n}\n\nasync function codemodComponent(\n workspace: Workspace,\n component: Component\n): Promise<{ files: SourceFile[]; warnings?: string[] }> {\n const issues = component.state.issues;\n const files: SourceFile[] = [];\n if (!issues || !issues.getIssue(IssuesClasses.RelativeComponentsAuthored)) return { files };\n const warnings: string[] = [];\n await Promise.all(\n component.filesystem.files.map(async (file: SourceFile) => {\n const relativeInstances = issues.getIssue(IssuesClasses.RelativeComponentsAuthored)?.data[\n pathNormalizeToLinux(file.relative)\n ];\n if (!relativeInstances) return;\n const fileBefore = file.contents.toString() as string;\n let newFileString = fileBefore;\n await Promise.all(\n relativeInstances.map(async (relativeEntry: RelativeComponentsAuthoredEntry) => {\n const id = relativeEntry.componentId;\n const requiredComponent = await workspace.get(id);\n const consumerComp = requiredComponent.state._consumer as ConsumerComponent;\n const packageName = componentIdToPackageName({ ...consumerComp, id });\n const cssFamily = ['.css', '.scss', '.less', '.sass'];\n const isCss = cssFamily.includes(file.extname);\n const packageNameSupportCss = isCss ? `~${packageName}` : packageName;\n const stringToReplace = getNameWithoutInternalPath(workspace, relativeEntry);\n // @todo: the \"dist\" should be replaced by the compiler dist-dir.\n // newFileString = replacePackageName(newFileString, stringToReplace, packageNameSupportCss, 'dist');\n newFileString = replacePackageName(newFileString, stringToReplace, packageNameSupportCss);\n })\n );\n if (fileBefore !== newFileString) {\n file.contents = Buffer.from(newFileString);\n files.push(file);\n }\n })\n );\n return { files, warnings };\n}\n\n/**\n * e.g.\n * importSource: '../workspace/workspace.ui'\n * sourceRelativePath: 'extensions/workspace/workspace.ui.tsx'\n * rootDir in .bitmap: 'extensions/workspace'.\n *\n * expected to return \"../workspace\", as this is the path to the package root without the internal path.\n *\n * eventually, only this string is replaced by the new package-name and the internal-path part\n * remains intact. ('../workspace/workspace.ui' => '@bit/workspace/workspace.ui').\n */\nfunction getNameWithoutInternalPath(workspace: Workspace, relativeEntry: RelativeComponentsAuthoredEntry): string {\n const importSource = relativeEntry.importSource;\n const componentMap = workspace.consumer.bitMap.getComponentIfExist(relativeEntry.componentId);\n if (!componentMap) return importSource;\n const rootDir = componentMap.rootDir;\n if (!rootDir) return importSource;\n const mainFile = pathJoinLinux(rootDir, componentMap.mainFile);\n const filePathRelativeToWorkspace = relativeEntry.relativePath.sourceRelativePath;\n if (filePathRelativeToWorkspace === mainFile) {\n return importSource;\n }\n // the importSource is not the main-file but an internal file, remove the internal part.\n const internalPath = pathRelativeLinux(rootDir, filePathRelativeToWorkspace);\n const removeLastOccurrence = (str, toRemove) => str.replace(new RegExp(`/${toRemove}$`), '');\n if (importSource.endsWith(internalPath)) {\n return removeLastOccurrence(importSource, internalPath);\n }\n const internalPathNoExt = internalPath.replace(path.extname(internalPath), '');\n if (importSource.endsWith(internalPathNoExt)) {\n return removeLastOccurrence(importSource, internalPathNoExt);\n }\n const internalPathNoIndex = removeLastOccurrence(internalPathNoExt, 'index');\n if (importSource.endsWith(internalPathNoIndex)) {\n return removeLastOccurrence(importSource, internalPathNoIndex);\n }\n\n // unable to find anything useful. just return the importSource.\n return importSource;\n}\n"],"mappings":";;;;;;AAAA,SAAAA,MAAA;EAAA,MAAAC,IAAA,GAAAC,sBAAA,CAAAC,OAAA;EAAAH,KAAA,YAAAA,CAAA;IAAA,OAAAC,IAAA;EAAA;EAAA,OAAAA,IAAA;AAAA;AAGA,SAAAG,iBAAA;EAAA,MAAAH,IAAA,GAAAE,OAAA;EAAAC,gBAAA,YAAAA,CAAA;IAAA,OAAAH,IAAA;EAAA;EAAA,OAAAA,IAAA;AAAA;AAGA,SAAAI,aAAA;EAAA,MAAAJ,IAAA,GAAAE,OAAA;EAAAE,YAAA,YAAAA,CAAA;IAAA,OAAAJ,IAAA;EAAA;EAAA,OAAAA,IAAA;AAAA;AACA,SAAAK,QAAA;EAAA,MAAAL,IAAA,GAAAE,OAAA;EAAAG,OAAA,YAAAA,CAAA;IAAA,OAAAL,IAAA;EAAA;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AON,YAAY;EACrB;EACA;EACA,MAAMW,YAAY,GAAG,IAAAC,2BAAiB,EAACP,OAAO,EAAEG,2BAA2B,CAAC;EAC5E,MAAMK,oBAAoB,GAAGA,CAACC,GAAG,EAAEC,QAAQ,KAAKD,GAAG,CAACE,OAAO,CAAC,IAAIC,MAAM,CAAC,IAAIF,QAAQ,GAAG,CAAC,EAAE,EAAE,CAAC;EAC5F,IAAIf,YAAY,CAACkB,QAAQ,CAACP,YAAY,CAAC,EAAE;IACvC,OAAOE,oBAAoB,CAACb,YAAY,EAAEW,YAAY,CAAC;EACzD;EACA,MAAMQ,iBAAiB,GAAGR,YAAY,CAACK,OAAO,CAACI,eAAI,CAAC5B,OAAO,CAACmB,YAAY,CAAC,EAAE,EAAE,CAAC;EAC9E,IAAIX,YAAY,CAACkB,QAAQ,CAACC,iBAAiB,CAAC,EAAE;IAC5C,OAAON,oBAAoB,CAACb,YAAY,EAAEmB,iBAAiB,CAAC;EAC9D;EACA,MAAME,mBAAmB,GAAGR,oBAAoB,CAACM,iBAAiB,EAAE,OAAO,CAAC;EAC5E,IAAInB,YAAY,CAACkB,QAAQ,CAACG,mBAAmB,CAAC,EAAE;IAC9C,OAAOR,oBAAoB,CAACb,YAAY,EAAEqB,mBAAmB,CAAC;EAChE;;EAEA;EACA,OAAOrB,YAAY;AACrB","ignoreList":[]}
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@teambit/workspace.modules.node-modules-linker",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.296",
|
|
4
4
|
"homepage": "https://bit.cloud/teambit/workspace/modules/node-modules-linker",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"componentId": {
|
|
7
7
|
"scope": "teambit.workspace",
|
|
8
8
|
"name": "modules/node-modules-linker",
|
|
9
|
-
"version": "0.0.
|
|
9
|
+
"version": "0.0.296"
|
|
10
10
|
},
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"fs-extra": "10.0.0",
|
|
@@ -20,11 +20,11 @@
|
|
|
20
20
|
"@teambit/legacy.logger": "0.0.24",
|
|
21
21
|
"@teambit/toolbox.path.path": "0.0.8",
|
|
22
22
|
"@teambit/workspace.root-components": "1.0.0",
|
|
23
|
-
"@teambit/component.sources": "0.0.
|
|
24
|
-
"@teambit/legacy.consumer-component": "0.0.
|
|
25
|
-
"@teambit/pkg.modules.component-package-name": "0.0.
|
|
26
|
-
"@teambit/legacy.bit-map": "0.0.
|
|
27
|
-
"@teambit/legacy.consumer": "0.0.
|
|
23
|
+
"@teambit/component.sources": "0.0.120",
|
|
24
|
+
"@teambit/legacy.consumer-component": "0.0.69",
|
|
25
|
+
"@teambit/pkg.modules.component-package-name": "0.0.75",
|
|
26
|
+
"@teambit/legacy.bit-map": "0.0.125",
|
|
27
|
+
"@teambit/legacy.consumer": "0.0.68"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@types/fs-extra": "9.0.7",
|