ember-codemod-v1-to-v2 0.6.3 → 0.7.1-alpha.1

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.
Files changed (47) hide show
  1. package/README.md +6 -1
  2. package/dist/bin/ember-codemod-v1-to-v2.js +37 -0
  3. package/{src → dist/src}/index.js +1 -2
  4. package/dist/src/migration/ember-addon/index.js +22 -0
  5. package/dist/src/migration/ember-addon/steps/analyze-addon.js +70 -0
  6. package/dist/src/migration/ember-addon/steps/create-files-from-blueprint.js +42 -0
  7. package/dist/src/migration/ember-addon/steps/create-options.js +73 -0
  8. package/dist/src/migration/ember-addon/steps/move-addon-files.js +59 -0
  9. package/dist/src/migration/ember-addon/steps/move-project-root-files.js +79 -0
  10. package/dist/src/migration/ember-addon/steps/move-test-app-files.js +65 -0
  11. package/dist/src/migration/ember-addon/steps/update-addon-package-json.js +138 -0
  12. package/dist/src/migration/ember-addon/steps/update-addon-tsconfig-json.js +26 -0
  13. package/dist/src/migration/ember-addon/steps/update-test-app-package-json.js +62 -0
  14. package/dist/src/migration/ember-addon/steps/update-test-app-tsconfig-json.js +30 -0
  15. package/dist/src/types/index.js +1 -0
  16. package/dist/src/utils/blueprints/blueprints-root.js +4 -0
  17. package/dist/src/utils/blueprints/get-version.js +25 -0
  18. package/{src → dist/src}/utils/json/sanitize-json.js +3 -4
  19. package/package.json +28 -14
  20. package/bin/ember-codemod-v1-to-v2.js +0 -42
  21. package/src/migration/ember-addon/index.js +0 -39
  22. package/src/migration/ember-addon/steps/analyze-addon.js +0 -82
  23. package/src/migration/ember-addon/steps/create-files-from-blueprint.js +0 -60
  24. package/src/migration/ember-addon/steps/create-options.js +0 -99
  25. package/src/migration/ember-addon/steps/move-addon-files.js +0 -84
  26. package/src/migration/ember-addon/steps/move-project-root-files.js +0 -110
  27. package/src/migration/ember-addon/steps/move-test-app-files.js +0 -90
  28. package/src/migration/ember-addon/steps/update-addon-package-json.js +0 -178
  29. package/src/migration/ember-addon/steps/update-addon-tsconfig-json.js +0 -38
  30. package/src/migration/ember-addon/steps/update-test-app-package-json.js +0 -95
  31. package/src/migration/ember-addon/steps/update-test-app-tsconfig-json.js +0 -43
  32. package/src/utils/blueprints/blueprints-root.js +0 -8
  33. package/src/utils/blueprints/get-version.js +0 -28
  34. /package/{src/blueprints → blueprints}/ember-addon/__addonLocation__/.eslintignore +0 -0
  35. /package/{src/blueprints → blueprints}/ember-addon/__addonLocation__/addon-main.cjs +0 -0
  36. /package/{src/blueprints → blueprints}/ember-addon/__addonLocation__/babel.config.json +0 -0
  37. /package/{src/blueprints → blueprints}/ember-addon/__addonLocation__/rollup.config.mjs +0 -0
  38. /package/{src/blueprints → blueprints}/ember-addon/__addonLocation__/unpublished-development-types/index.d.ts +0 -0
  39. /package/{src/blueprints → blueprints}/ember-addon/__gitignore__ +0 -0
  40. /package/{src/blueprints → blueprints}/ember-addon/__testAppLocation__/ember-cli-build.js +0 -0
  41. /package/{src/blueprints → blueprints}/ember-addon/__testAppLocation__/types/global.d.ts +0 -0
  42. /package/{src/blueprints → blueprints}/ember-addon/package.json +0 -0
  43. /package/{src/blueprints → blueprints}/ember-addon/pnpm-workspace.yaml +0 -0
  44. /package/{src → dist/src}/migration/ember-addon/steps/index.js +0 -0
  45. /package/{src → dist/src}/migration/index.js +0 -0
  46. /package/{src → dist/src}/utils/blueprints.js +0 -0
  47. /package/{src → dist/src}/utils/json.js +0 -0
package/README.md CHANGED
@@ -102,7 +102,12 @@ To better meet your needs, consider cloning the repo and running the codemod loc
102
102
 
103
103
  ```sh
104
104
  cd <your/cloned/repo>
105
- ./bin/ember-codemod-v1-to-v2.js --root=<your/project/path>
105
+
106
+ # Compile TypeScript
107
+ pnpm build
108
+
109
+ # Run codemod
110
+ ./dist/bin/ember-codemod-v1-to-v2.js --root=<your/project/path>
106
111
  ```
107
112
 
108
113
  You can also look at another codemod called [`ember-addon-migrator`](https://github.com/NullVoxPopuli/ember-addon-migrator).
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/env node
2
+ // eslint-disable-next-line n/shebang
3
+ 'use strict';
4
+ import yargs from 'yargs';
5
+ import { hideBin } from 'yargs/helpers';
6
+ import { runCodemod } from '../src/index.js';
7
+ // Provide a title to the process in `ps`
8
+ process.title = 'ember-codemod-v1-to-v2';
9
+ // Set codemod options
10
+ const argv = yargs(hideBin(process.argv))
11
+ .option('addon-location', {
12
+ describe: 'Location of the addon package',
13
+ type: 'string',
14
+ })
15
+ .option('root', {
16
+ describe: 'Location of your Ember addon',
17
+ type: 'string',
18
+ })
19
+ .option('test-app-location', {
20
+ describe: 'Location of the test-app package',
21
+ type: 'string',
22
+ })
23
+ .option('test-app-name', {
24
+ describe: 'Name of the test-app package',
25
+ type: 'string',
26
+ })
27
+ .parseSync();
28
+ function castEmptyStringToUndefined(value) {
29
+ return value === '' ? undefined : value;
30
+ }
31
+ const codemodOptions = {
32
+ addonLocation: castEmptyStringToUndefined(argv['addon-location']),
33
+ projectRoot: argv['root'] ?? process.cwd(),
34
+ testAppLocation: castEmptyStringToUndefined(argv['test-app-location']),
35
+ testAppName: castEmptyStringToUndefined(argv['test-app-name']),
36
+ };
37
+ runCodemod(codemodOptions);
@@ -1,5 +1,4 @@
1
1
  import { migrateEmberAddon } from './migration/index.js';
2
-
3
2
  export function runCodemod(codemodOptions) {
4
- migrateEmberAddon(codemodOptions);
3
+ migrateEmberAddon(codemodOptions);
5
4
  }
@@ -0,0 +1,22 @@
1
+ import { analyzeAddon, createFilesFromBlueprint, createOptions, moveAddonFiles, moveProjectRootFiles, moveTestAppFiles, updateAddonPackageJson, updateAddonTsConfigJson, updateTestAppPackageJson, updateTestAppTsConfigJson, } from './steps/index.js';
2
+ export function migrateEmberAddon(codemodOptions) {
3
+ const options = createOptions(codemodOptions);
4
+ const { isV1Addon } = options.packages.addon;
5
+ // Guarantee idempotency
6
+ if (!isV1Addon) {
7
+ return;
8
+ }
9
+ // Prepare for migration
10
+ const context = analyzeAddon(options);
11
+ // Preserve code
12
+ moveAddonFiles(options);
13
+ moveTestAppFiles(options);
14
+ moveProjectRootFiles(options);
15
+ // Get the latest code from blueprint
16
+ createFilesFromBlueprint(context, options);
17
+ // Fine-tune individual files
18
+ updateAddonPackageJson(context, options);
19
+ updateAddonTsConfigJson(options);
20
+ updateTestAppPackageJson(options);
21
+ updateTestAppTsConfigJson(options);
22
+ }
@@ -0,0 +1,70 @@
1
+ import { findFiles, renamePathByDirectory } from '@codemod-utils/files';
2
+ import { getVersion } from '../../../utils/blueprints.js';
3
+ function getAppReexports(options) {
4
+ const { projectRoot } = options;
5
+ const filePaths = findFiles('app/**/*.js', {
6
+ projectRoot,
7
+ });
8
+ return filePaths
9
+ .map((filePath) => {
10
+ return renamePathByDirectory(filePath, {
11
+ from: 'app',
12
+ to: '',
13
+ });
14
+ })
15
+ .sort();
16
+ }
17
+ function getProjectRootDevDependencies(options) {
18
+ return {
19
+ concurrently: getVersion('concurrently', options),
20
+ };
21
+ }
22
+ function getPublicAssets(options) {
23
+ const { projectRoot } = options;
24
+ const filePaths = findFiles('public/**/*', {
25
+ projectRoot,
26
+ });
27
+ return filePaths
28
+ .map((filePath) => {
29
+ return renamePathByDirectory(filePath, {
30
+ from: 'public',
31
+ to: '',
32
+ });
33
+ })
34
+ .sort();
35
+ }
36
+ function getPublicEntrypoints(options) {
37
+ const { projectRoot } = options;
38
+ const filePaths = findFiles('{addon,addon-test-support}/**/*.{js,ts}', {
39
+ projectRoot,
40
+ });
41
+ return filePaths
42
+ .map((filePath) => {
43
+ return renamePathByDirectory(filePath, {
44
+ from: 'addon',
45
+ to: '',
46
+ });
47
+ })
48
+ .map((filePath) => {
49
+ return renamePathByDirectory(filePath, {
50
+ from: 'addon-test-support',
51
+ to: 'test-support',
52
+ });
53
+ })
54
+ .map((filePath) => {
55
+ return filePath.replace(/ts$/, 'js');
56
+ })
57
+ .sort();
58
+ }
59
+ export function analyzeAddon(options) {
60
+ return {
61
+ addon: {
62
+ appReexports: getAppReexports(options),
63
+ publicAssets: getPublicAssets(options),
64
+ publicEntrypoints: getPublicEntrypoints(options),
65
+ },
66
+ projectRoot: {
67
+ devDependencies: getProjectRootDevDependencies(options),
68
+ },
69
+ };
70
+ }
@@ -0,0 +1,42 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ import { processTemplate } from '@codemod-utils/blueprints';
4
+ import { createFiles, findFiles } from '@codemod-utils/files';
5
+ import { blueprintsRoot } from '../../../utils/blueprints.js';
6
+ function getFilesToSkip(options) {
7
+ const { packageManager, packages } = options;
8
+ const files = new Set();
9
+ if (!packages.addon.hasTypeScript) {
10
+ files.add('__addonLocation__/unpublished-development-types/index.d.ts');
11
+ files.add('__testAppLocation__/types/global.d.ts');
12
+ }
13
+ if (!packageManager.isPnpm) {
14
+ files.add('pnpm-workspace.yaml');
15
+ }
16
+ return [...files];
17
+ }
18
+ function resolveBlueprintFilePath(blueprintFilePath, options) {
19
+ const { locations } = options;
20
+ return blueprintFilePath
21
+ .replace('__addonLocation__', locations.addon)
22
+ .replace('__gitignore__', '.gitignore')
23
+ .replace('__testAppLocation__', locations.testApp);
24
+ }
25
+ export function createFilesFromBlueprint(context, options) {
26
+ const filesToSkip = getFilesToSkip(options);
27
+ const cwd = join(blueprintsRoot, 'ember-addon');
28
+ const blueprintFilePaths = findFiles('**/*', {
29
+ ignoreList: filesToSkip,
30
+ projectRoot: cwd,
31
+ });
32
+ const fileMap = new Map(blueprintFilePaths.map((blueprintFilePath) => {
33
+ const filePath = resolveBlueprintFilePath(blueprintFilePath, options);
34
+ const blueprintFile = readFileSync(join(cwd, blueprintFilePath), 'utf8');
35
+ const file = processTemplate(blueprintFile, {
36
+ context,
37
+ options,
38
+ });
39
+ return [filePath, file];
40
+ }));
41
+ createFiles(fileMap, options);
42
+ }
@@ -0,0 +1,73 @@
1
+ import { findFiles, unionize } from '@codemod-utils/files';
2
+ import { readPackageJson, validatePackageJson } from '@codemod-utils/json';
3
+ function analyzePackageJson(codemodOptions) {
4
+ const { projectRoot } = codemodOptions;
5
+ const packageJson = readPackageJson({ projectRoot });
6
+ validatePackageJson(packageJson);
7
+ const { dependencies, devDependencies, 'ember-addon': emberAddon, name, version, } = packageJson;
8
+ const projectDependencies = new Map([
9
+ ...Object.entries(dependencies ?? {}),
10
+ ...Object.entries(devDependencies ?? {}),
11
+ ]);
12
+ return {
13
+ dependencies: projectDependencies,
14
+ hasGlint: projectDependencies.has('@glint/core'),
15
+ hasTypeScript: projectDependencies.has('typescript'),
16
+ isV1Addon: Boolean(emberAddon),
17
+ name,
18
+ version,
19
+ };
20
+ }
21
+ function analyzePackageManager(codemodOptions) {
22
+ const { projectRoot } = codemodOptions;
23
+ const mapping = new Map([
24
+ ['package-lock.json', 'npm'],
25
+ ['pnpm-lock.yaml', 'pnpm'],
26
+ ['yarn.lock', 'yarn'],
27
+ ]);
28
+ const lockFiles = [...mapping.keys()];
29
+ const filePaths = findFiles(unionize(lockFiles), {
30
+ projectRoot,
31
+ });
32
+ if (filePaths.length !== 1) {
33
+ console.warn('WARNING: Package manager is unknown. Yarn will be assumed.');
34
+ return {
35
+ isNpm: false,
36
+ isPnpm: false,
37
+ isYarn: true,
38
+ };
39
+ }
40
+ const packageManager = mapping.get(filePaths[0]);
41
+ return {
42
+ isNpm: packageManager === 'npm',
43
+ isPnpm: packageManager === 'pnpm',
44
+ isYarn: packageManager === 'yarn',
45
+ };
46
+ }
47
+ function deriveAddonLocation(addonPackage) {
48
+ const hasScope = addonPackage.name.includes('/');
49
+ if (!hasScope) {
50
+ return addonPackage.name;
51
+ }
52
+ // eslint-disable-next-line no-unused-vars
53
+ const [_scope, packageName] = addonPackage.name.split('/');
54
+ return packageName;
55
+ }
56
+ export function createOptions(codemodOptions) {
57
+ const addonPackage = analyzePackageJson(codemodOptions);
58
+ const packageManager = analyzePackageManager(codemodOptions);
59
+ return {
60
+ locations: {
61
+ addon: codemodOptions.addonLocation ?? deriveAddonLocation(addonPackage),
62
+ testApp: codemodOptions.testAppLocation ?? 'test-app',
63
+ },
64
+ packageManager,
65
+ packages: {
66
+ addon: addonPackage,
67
+ testApp: {
68
+ name: codemodOptions.testAppName ?? 'test-app',
69
+ },
70
+ },
71
+ projectRoot: codemodOptions.projectRoot,
72
+ };
73
+ }
@@ -0,0 +1,59 @@
1
+ import { findFiles, mapFilePaths, moveFiles, removeFiles, } from '@codemod-utils/files';
2
+ function moveAddonFolder(options) {
3
+ const { locations, projectRoot } = options;
4
+ const filePaths = findFiles('addon/**/*', {
5
+ projectRoot,
6
+ });
7
+ const filePathMap = mapFilePaths(filePaths, {
8
+ from: 'addon',
9
+ to: `${locations.addon}/src`,
10
+ });
11
+ moveFiles(filePathMap, options);
12
+ }
13
+ function moveAddonTestSupportFolder(options) {
14
+ const { locations, projectRoot } = options;
15
+ const filePaths = findFiles('addon-test-support/**/*', {
16
+ projectRoot,
17
+ });
18
+ const filePathMap = mapFilePaths(filePaths, {
19
+ from: 'addon-test-support',
20
+ to: `${locations.addon}/src/test-support`,
21
+ });
22
+ moveFiles(filePathMap, options);
23
+ }
24
+ function moveBlueprintsFolder(options) {
25
+ const { locations, projectRoot } = options;
26
+ const filePaths = findFiles('blueprints/**/*', {
27
+ projectRoot,
28
+ });
29
+ const filePathMap = mapFilePaths(filePaths, {
30
+ from: 'blueprints',
31
+ to: `${locations.addon}/blueprints`,
32
+ });
33
+ moveFiles(filePathMap, options);
34
+ }
35
+ function movePublicFolder(options) {
36
+ const { locations, projectRoot } = options;
37
+ const filePaths = findFiles('public/**/*', {
38
+ projectRoot,
39
+ });
40
+ const filePathMap = mapFilePaths(filePaths, {
41
+ from: 'public',
42
+ to: `${locations.addon}/public`,
43
+ });
44
+ moveFiles(filePathMap, options);
45
+ }
46
+ function removeAppFolder(options) {
47
+ const { projectRoot } = options;
48
+ const filePaths = findFiles('app/**/*', {
49
+ projectRoot,
50
+ });
51
+ removeFiles(filePaths, options);
52
+ }
53
+ export function moveAddonFiles(options) {
54
+ moveAddonFolder(options);
55
+ moveAddonTestSupportFolder(options);
56
+ moveBlueprintsFolder(options);
57
+ movePublicFolder(options);
58
+ removeAppFolder(options);
59
+ }
@@ -0,0 +1,79 @@
1
+ import { copyFiles, findFiles, mapFilePaths, moveFiles, removeFiles, unionize, } from '@codemod-utils/files';
2
+ function copyToAddon(options) {
3
+ const { locations, projectRoot } = options;
4
+ const files = ['LICENSE.md', 'README.md'];
5
+ const filePaths = findFiles(unionize(files), {
6
+ projectRoot,
7
+ });
8
+ const filePathMap = mapFilePaths(filePaths, {
9
+ from: '',
10
+ to: locations.addon,
11
+ });
12
+ copyFiles(filePathMap, options);
13
+ }
14
+ function moveToAddonAndTestApp(options) {
15
+ const { locations, packages, projectRoot } = options;
16
+ const files = new Set([
17
+ '.eslintignore',
18
+ '.eslintrc.cjs',
19
+ '.eslintrc.js',
20
+ '.gitignore',
21
+ '.prettierignore',
22
+ '.prettierrc.cjs',
23
+ '.prettierrc.js',
24
+ '.stylelintignore',
25
+ '.stylelintrc.cjs',
26
+ '.stylelintrc.js',
27
+ '.template-lintrc.cjs',
28
+ '.template-lintrc.js',
29
+ 'package.json',
30
+ ]);
31
+ if (packages.addon.hasTypeScript) {
32
+ files.add('tsconfig.json');
33
+ }
34
+ const filePaths = findFiles(unionize([...files]), {
35
+ projectRoot,
36
+ });
37
+ let filePathMap = mapFilePaths(filePaths, {
38
+ from: '',
39
+ to: locations.addon,
40
+ });
41
+ copyFiles(filePathMap, options);
42
+ filePathMap = mapFilePaths(filePaths, {
43
+ from: '',
44
+ to: locations.testApp,
45
+ });
46
+ copyFiles(filePathMap, options);
47
+ removeFiles(filePaths, options);
48
+ }
49
+ function moveToTestApp(options) {
50
+ const { locations, projectRoot } = options;
51
+ const files = [
52
+ '.ember-cli',
53
+ '.watchmanconfig',
54
+ 'ember-cli-build.js',
55
+ 'testem.js',
56
+ ];
57
+ const filePaths = findFiles(unionize(files), {
58
+ projectRoot,
59
+ });
60
+ const filePathMap = mapFilePaths(filePaths, {
61
+ from: '',
62
+ to: locations.testApp,
63
+ });
64
+ moveFiles(filePathMap, options);
65
+ }
66
+ function removeFromProjectRoot(options) {
67
+ const { projectRoot } = options;
68
+ const files = ['.npmignore', 'index.js'];
69
+ const filePaths = findFiles(unionize(files), {
70
+ projectRoot,
71
+ });
72
+ removeFiles(filePaths, options);
73
+ }
74
+ export function moveProjectRootFiles(options) {
75
+ copyToAddon(options);
76
+ moveToAddonAndTestApp(options);
77
+ moveToTestApp(options);
78
+ removeFromProjectRoot(options);
79
+ }
@@ -0,0 +1,65 @@
1
+ import { readFileSync, writeFileSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ import { findFiles, mapFilePaths, moveFiles } from '@codemod-utils/files';
4
+ function moveTestsFolder(options) {
5
+ const { locations, projectRoot } = options;
6
+ let filePaths = findFiles('tests/dummy/**/*', {
7
+ projectRoot,
8
+ });
9
+ let filePathMap = mapFilePaths(filePaths, {
10
+ from: 'tests/dummy',
11
+ to: locations.testApp,
12
+ });
13
+ moveFiles(filePathMap, options);
14
+ filePaths = findFiles('tests/**/*', {
15
+ ignoreList: ['tests/dummy/**/*'],
16
+ projectRoot,
17
+ });
18
+ filePathMap = mapFilePaths(filePaths, {
19
+ from: 'tests',
20
+ to: `${locations.testApp}/tests`,
21
+ });
22
+ moveFiles(filePathMap, options);
23
+ }
24
+ function moveTypesFolder(options) {
25
+ const { locations, packages, projectRoot } = options;
26
+ if (!packages.addon.hasTypeScript) {
27
+ return;
28
+ }
29
+ let filePaths = findFiles('types/dummy/**/*', {
30
+ projectRoot,
31
+ });
32
+ let filePathMap = mapFilePaths(filePaths, {
33
+ from: 'types/dummy',
34
+ to: `${locations.testApp}/types/${packages.testApp.name}`,
35
+ });
36
+ moveFiles(filePathMap, options);
37
+ filePaths = findFiles('types/**/*', {
38
+ ignoreList: ['types/dummy/**/*'],
39
+ projectRoot,
40
+ });
41
+ filePathMap = mapFilePaths(filePaths, {
42
+ from: 'types',
43
+ to: `${locations.testApp}/types`,
44
+ });
45
+ moveFiles(filePathMap, options);
46
+ }
47
+ function renameDummy(options) {
48
+ const { locations, packages, projectRoot } = options;
49
+ // File extensions had been specified, partly to encode assumptions
50
+ // about Ember, and partly to avoid corrupting non-text files
51
+ const filePaths = findFiles(`${locations.testApp}/**/*.{d.ts,html,js,ts}`, {
52
+ projectRoot,
53
+ });
54
+ filePaths.forEach((filePath) => {
55
+ const oldPath = join(projectRoot, filePath);
56
+ const oldFile = readFileSync(oldPath, 'utf8');
57
+ const newFile = oldFile.replace(new RegExp('dummy', 'g'), packages.testApp.name);
58
+ writeFileSync(oldPath, newFile, 'utf8');
59
+ });
60
+ }
61
+ export function moveTestAppFiles(options) {
62
+ moveTestsFolder(options);
63
+ moveTypesFolder(options);
64
+ renameDummy(options);
65
+ }
@@ -0,0 +1,138 @@
1
+ import { writeFileSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ import { convertToMap, convertToObject, readPackageJson, } from '@codemod-utils/json';
4
+ import { getVersion } from '../../../utils/blueprints.js';
5
+ function updateDependencies(packageJson, options) {
6
+ const dependencies = convertToMap(packageJson['dependencies']);
7
+ const packagesToDelete = [
8
+ '@embroider/macros',
9
+ 'ember-auto-import',
10
+ 'ember-cli-babel',
11
+ 'ember-cli-htmlbars',
12
+ ];
13
+ packagesToDelete.forEach((packageName) => {
14
+ dependencies.delete(packageName);
15
+ });
16
+ const packagesToInstall = ['@embroider/addon-shim'];
17
+ packagesToInstall.sort().forEach((packageName) => {
18
+ const version = getVersion(packageName, options);
19
+ dependencies.set(packageName, version);
20
+ });
21
+ packageJson['dependencies'] = convertToObject(dependencies);
22
+ }
23
+ function updateDevDependencies(packageJson, options) {
24
+ const { packages } = options;
25
+ const devDependencies = convertToMap(packageJson['devDependencies']);
26
+ /*
27
+ For the time being, we'll take the approach of starting over and
28
+ adding back the development dependencies that are required. For
29
+ a more conservative approach, we could delete only the following:
30
+
31
+ - @embroider/macros
32
+ - ember-auto-import
33
+ - ember-cli-babel
34
+ - ember-cli-htmlbars
35
+ */
36
+ devDependencies.clear();
37
+ const packagesToInstall = new Set([
38
+ '@babel/core',
39
+ '@babel/plugin-proposal-class-properties',
40
+ '@babel/plugin-proposal-decorators',
41
+ '@babel/runtime',
42
+ '@embroider/addon-dev',
43
+ '@rollup/plugin-babel',
44
+ 'rollup',
45
+ 'rollup-plugin-copy',
46
+ ]);
47
+ if (packages.addon.hasTypeScript) {
48
+ packagesToInstall.add('@babel/preset-typescript');
49
+ packagesToInstall.add('rollup-plugin-ts');
50
+ packagesToInstall.delete('@rollup/plugin-babel');
51
+ }
52
+ [...packagesToInstall].sort().forEach((packageName) => {
53
+ const version = getVersion(packageName, options);
54
+ devDependencies.set(packageName, version);
55
+ });
56
+ packageJson['devDependencies'] = convertToObject(devDependencies);
57
+ }
58
+ function updateOtherFields(packageJson, context, options) {
59
+ const { packages } = options;
60
+ const hasPublicAssets = context.addon.publicAssets.length > 0;
61
+ if (hasPublicAssets) {
62
+ const publicAssetMapping = context.addon.publicAssets.reduce((accumulator, filePath) => {
63
+ const from = `./public/${filePath}`;
64
+ const to = `/${packages.addon.name}/${filePath}`;
65
+ accumulator[from] = to;
66
+ return accumulator;
67
+ }, {});
68
+ packageJson['ember-addon'] = {
69
+ 'app-js': {},
70
+ main: 'addon-main.cjs',
71
+ 'public-assets': publicAssetMapping,
72
+ type: 'addon',
73
+ version: 2,
74
+ };
75
+ }
76
+ else {
77
+ packageJson['ember-addon'] = {
78
+ 'app-js': {},
79
+ main: 'addon-main.cjs',
80
+ type: 'addon',
81
+ version: 2,
82
+ };
83
+ }
84
+ if (packages.addon.hasTypeScript) {
85
+ packageJson['exports'] = {
86
+ '.': './dist/index.js',
87
+ './*': {
88
+ /*
89
+ This object has an order dependency. The `default` key must appear last.
90
+ */
91
+ types: './dist/*.d.ts',
92
+ default: './dist/*.js',
93
+ },
94
+ './addon-main.js': './addon-main.cjs',
95
+ };
96
+ }
97
+ else {
98
+ packageJson['exports'] = {
99
+ '.': './dist/index.js',
100
+ './*': './dist/*.js',
101
+ './addon-main.js': './addon-main.cjs',
102
+ };
103
+ }
104
+ if (hasPublicAssets) {
105
+ packageJson['files'] = ['addon-main.cjs', 'dist', 'public'];
106
+ }
107
+ else {
108
+ packageJson['files'] = ['addon-main.cjs', 'dist'];
109
+ }
110
+ if (packages.addon.hasTypeScript) {
111
+ packageJson['typesVersions'] = {
112
+ '*': {
113
+ '*': ['dist/*'],
114
+ },
115
+ };
116
+ }
117
+ }
118
+ function updateScripts(packageJson) {
119
+ const scripts = convertToMap(packageJson['scripts']);
120
+ scripts.set('build', 'rollup --config');
121
+ scripts.set('prepack', 'rollup --config');
122
+ scripts.set('start', 'rollup --config --watch');
123
+ scripts.set('test', "echo 'A v2 addon does not have tests, run tests in test-app'");
124
+ packageJson['scripts'] = convertToObject(scripts);
125
+ }
126
+ export function updateAddonPackageJson(context, options) {
127
+ const { locations, projectRoot } = options;
128
+ const packageJson = readPackageJson({
129
+ projectRoot: join(projectRoot, locations.addon),
130
+ });
131
+ updateDependencies(packageJson, options);
132
+ updateDevDependencies(packageJson, options);
133
+ updateScripts(packageJson);
134
+ updateOtherFields(packageJson, context, options);
135
+ const destination = join(projectRoot, locations.addon, 'package.json');
136
+ const file = JSON.stringify(packageJson, null, 2) + '\n';
137
+ writeFileSync(destination, file, 'utf8');
138
+ }
@@ -0,0 +1,26 @@
1
+ import { readFileSync, writeFileSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ import { convertToMap, convertToObject } from '@codemod-utils/json';
4
+ import { sanitizeJson } from '../../../utils/json.js';
5
+ function updateCompilerOptions(tsConfigJson) {
6
+ const compilerOptions = convertToMap(tsConfigJson['compilerOptions']);
7
+ compilerOptions.delete('baseUrl');
8
+ compilerOptions.delete('paths');
9
+ tsConfigJson['compilerOptions'] = convertToObject(compilerOptions);
10
+ }
11
+ function updateInclude(tsConfigJson) {
12
+ tsConfigJson['include'] = ['src/**/*', 'unpublished-development-types/**/*'];
13
+ }
14
+ export function updateAddonTsConfigJson(options) {
15
+ const { locations, packages, projectRoot } = options;
16
+ if (!packages.addon.hasTypeScript) {
17
+ return;
18
+ }
19
+ const oldPath = join(projectRoot, locations.addon, 'tsconfig.json');
20
+ const oldFile = readFileSync(oldPath, 'utf8');
21
+ const tsConfigJson = JSON.parse(sanitizeJson(oldFile));
22
+ updateCompilerOptions(tsConfigJson);
23
+ updateInclude(tsConfigJson);
24
+ const newFile = JSON.stringify(tsConfigJson, null, 2) + '\n';
25
+ writeFileSync(oldPath, newFile, 'utf8');
26
+ }