@teambit/workspace.modules.node-modules-linker 0.0.273 → 0.0.275
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
|
@@ -116,7 +116,7 @@ function getNameWithoutInternalPath(workspace: Workspace, relativeEntry: Relativ
|
|
|
116
116
|
if (!componentMap) return importSource;
|
|
117
117
|
const rootDir = componentMap.rootDir;
|
|
118
118
|
if (!rootDir) return importSource;
|
|
119
|
-
const mainFile =
|
|
119
|
+
const mainFile = pathJoinLinux(rootDir, componentMap.mainFile);
|
|
120
120
|
const filePathRelativeToWorkspace = relativeEntry.relativePath.sourceRelativePath;
|
|
121
121
|
if (filePathRelativeToWorkspace === mainFile) {
|
|
122
122
|
return importSource;
|
|
@@ -141,7 +141,7 @@ function getNameWithoutInternalPath(workspace, relativeEntry) {
|
|
|
141
141
|
if (!componentMap) return importSource;
|
|
142
142
|
const rootDir = componentMap.rootDir;
|
|
143
143
|
if (!rootDir) return importSource;
|
|
144
|
-
const mainFile =
|
|
144
|
+
const mainFile = (0, _legacy().pathJoinLinux)(rootDir, componentMap.mainFile);
|
|
145
145
|
const filePathRelativeToWorkspace = relativeEntry.relativePath.sourceRelativePath;
|
|
146
146
|
if (filePathRelativeToWorkspace === mainFile) {
|
|
147
147
|
return importSource;
|
|
@@ -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","trackDir","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 { Workspace } from '@teambit/workspace';\nimport { IssuesClasses, RelativeComponentsAuthoredEntry } from '@teambit/component-issues';\nimport { Component } from '@teambit/component';\nimport { ComponentID, ComponentIdList } from '@teambit/component-id';\nimport { pathJoinLinux, pathNormalizeToLinux, pathRelativeLinux, replacePackageName } from '@teambit/legacy.utils';\nimport { componentIdToPackageName } from '@teambit/pkg.modules.component-package-name';\nimport { DataToPersist, SourceFile } from '@teambit/component.sources';\nimport { 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 = componentMap.trackDir ? componentMap.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;EAA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IAC9C,OAAOR,oBAAoB,CAACd,YAAY,EAAEsB,mBAAmB,CAAC;EAChE;;EAEA;EACA,OAAOtB,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 { Workspace } from '@teambit/workspace';\nimport { IssuesClasses, RelativeComponentsAuthoredEntry } from '@teambit/component-issues';\nimport { Component } from '@teambit/component';\nimport { ComponentID, ComponentIdList } from '@teambit/component-id';\nimport { pathJoinLinux, pathNormalizeToLinux, pathRelativeLinux, replacePackageName } from '@teambit/legacy.utils';\nimport { componentIdToPackageName } from '@teambit/pkg.modules.component-package-name';\nimport { DataToPersist, SourceFile } from '@teambit/component.sources';\nimport { 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;AAEA,SAAAG,iBAAA;EAAA,MAAAH,IAAA,GAAAE,OAAA;EAAAC,gBAAA,YAAAA,CAAA;IAAA,OAAAH,IAAA;EAAA;EAAA,OAAAA,IAAA;AAAA;AAEA,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;AACA,SAAAO,WAAA;EAAA,MAAAP,IAAA,GAAAE,OAAA;EAAAK,UAAA,YAAAA,CAAA;IAAA,OAAAP,IAAA;EAAA;EAAA,OAAAA,IAAA;AAAA;AAAuE,SAAAC,uBAAAO,CAAA,WAAAA,CAAA,IAAAA,CAAA,CA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b,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.275",
|
|
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.275"
|
|
10
10
|
},
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"fs-extra": "10.0.0",
|
|
@@ -15,14 +15,14 @@
|
|
|
15
15
|
"@teambit/component-id": "1.2.3",
|
|
16
16
|
"@teambit/workspace.root-components": "1.0.0",
|
|
17
17
|
"@teambit/component-issues": "0.0.157",
|
|
18
|
-
"@teambit/component.sources": "0.0.
|
|
19
|
-
"@teambit/legacy.consumer-component": "0.0.
|
|
18
|
+
"@teambit/component.sources": "0.0.99",
|
|
19
|
+
"@teambit/legacy.consumer-component": "0.0.48",
|
|
20
20
|
"@teambit/legacy.utils": "0.0.19",
|
|
21
|
-
"@teambit/pkg.modules.component-package-name": "0.0.
|
|
21
|
+
"@teambit/pkg.modules.component-package-name": "0.0.54",
|
|
22
22
|
"@teambit/component-package-version": "0.0.438",
|
|
23
|
-
"@teambit/legacy.bit-map": "0.0.
|
|
23
|
+
"@teambit/legacy.bit-map": "0.0.104",
|
|
24
24
|
"@teambit/legacy.constants": "0.0.11",
|
|
25
|
-
"@teambit/legacy.consumer": "0.0.
|
|
25
|
+
"@teambit/legacy.consumer": "0.0.47",
|
|
26
26
|
"@teambit/legacy.logger": "0.0.17",
|
|
27
27
|
"@teambit/toolbox.path.path": "0.0.8"
|
|
28
28
|
},
|