Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reorganized utilities and handled multiline comments in JSON #10

Merged
merged 10 commits into from
Jan 23, 2023
Merged
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"@babel/eslint-parser": "^7.19.1",
"glob": "^8.1.0",
"lodash.template": "^4.5.0",
"strip-json-comments": "^5.0.0",
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove this dependency if we can find another (simple) way to partially update tsconfig.json.

"yargs": "^17.6.2"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions src/blueprints/ember-addon/__addonLocation__/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ export default {
plugins: [
// These are the modules that users should be able to import from your
// addon. Anything not listed here may get optimized away.
addon.publicEntrypoints([<%= context.publicEntrypoints.map((filePath) => `'${filePath}'`).join(', ') %>]),
addon.publicEntrypoints([<%= context.addon.publicEntrypoints.map((filePath) => `'${filePath}'`).join(', ') %>]),

// These are the modules that should get reexported into the traditional
// "app" tree. Things in here should also be in publicEntrypoints above, but
// not everything in publicEntrypoints necessarily needs to go here.
addon.appReexports([<%= context.appReexports.map((filePath) => `'${filePath}'`).join(', ') %>]),
addon.appReexports([<%= context.addon.appReexports.map((filePath) => `'${filePath}'`).join(', ') %>]),

// Follow the V2 Addon rules about dependencies. Your code can import from
// `dependencies` and `peerDependencies` as well as standard Ember-provided
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ export default {
plugins: [
// These are the modules that users should be able to import from your
// addon. Anything not listed here may get optimized away.
addon.publicEntrypoints([<%= context.publicEntrypoints.map((filePath) => `'${filePath}'`).join(', ') %>]),
addon.publicEntrypoints([<%= context.addon.publicEntrypoints.map((filePath) => `'${filePath}'`).join(', ') %>]),

// These are the modules that should get reexported into the traditional
// "app" tree. Things in here should also be in publicEntrypoints above, but
// not everything in publicEntrypoints necessarily needs to go here.
addon.appReexports([<%= context.appReexports.map((filePath) => `'${filePath}'`).join(', ') %>]),
addon.appReexports([<%= context.addon.appReexports.map((filePath) => `'${filePath}'`).join(', ') %>]),

// Follow the V2 Addon rules about dependencies. Your code can import from
// `dependencies` and `peerDependencies` as well as standard Ember-provided
Expand Down
12 changes: 6 additions & 6 deletions src/blueprints/ember-addon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
"test": "npm run test --workspaces --if-present"
},
"devDependencies": {
"concurrently": "<%= options.packages.addon.dependencies.get('concurrently') ?? '^7.6.0' %>",
"prettier": "<%= options.packages.addon.dependencies.get('prettier') ?? '^2.8.3' %>"
"concurrently": "<%= context.projectRoot.devDependencies['concurrently'] %>",
"prettier": "<%= context.projectRoot.devDependencies['prettier'] %>"
Comment on lines +23 to +24
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed a technical risk (hard-coded latest versions).

}
}<% } else if (options.packageManager.isPnpm) { %>{
"name": "<%= options.packages.addon.name %>",
Expand All @@ -45,8 +45,8 @@
"test": "pnpm --filter '*' test"
},
"devDependencies": {
"concurrently": "<%= options.packages.addon.dependencies.get('concurrently') ?? '^7.6.0' %>",
"prettier": "<%= options.packages.addon.dependencies.get('prettier') ?? '^2.8.3' %>"
"concurrently": "<%= context.projectRoot.devDependencies['concurrently'] %>",
"prettier": "<%= context.projectRoot.devDependencies['prettier'] %>"
}
}<% } else if (options.packageManager.isYarn) { %>{
"name": "<%= options.packages.addon.name %>",
Expand All @@ -70,7 +70,7 @@
"test": "yarn workspaces run test"
},
"devDependencies": {
"concurrently": "<%= options.packages.addon.dependencies.get('concurrently') ?? '^7.6.0' %>",
"prettier": "<%= options.packages.addon.dependencies.get('prettier') ?? '^2.8.3' %>"
"concurrently": "<%= context.projectRoot.devDependencies['concurrently'] %>",
"prettier": "<%= context.projectRoot.devDependencies['prettier'] %>"
}
}<% } %>
22 changes: 18 additions & 4 deletions src/migration/ember-addon/steps/analyze-addon.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { join } from 'node:path';

import glob from 'glob';

import { decideVersion } from '../../../utils/blueprints.js';

function getAppReexports(options) {
const { projectRoot } = options;

Expand All @@ -12,6 +14,13 @@ function getAppReexports(options) {
return filePaths;
}

function getProjectRootDevDependencies(options) {
return {
concurrently: decideVersion('concurrently', options),
prettier: decideVersion('prettier', options),
};
}

function getPublicEntrypoints(options) {
const { projectRoot } = options;

Expand All @@ -23,8 +32,13 @@ function getPublicEntrypoints(options) {
}

export function analyzeAddon(options) {
const appReexports = getAppReexports(options);
const publicEntrypoints = getPublicEntrypoints(options);

return { appReexports, publicEntrypoints };
return {
addon: {
appReexports: getAppReexports(options),
publicEntrypoints: getPublicEntrypoints(options),
},
projectRoot: {
devDependencies: getProjectRootDevDependencies(options),
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { fileURLToPath } from 'node:url';

import glob from 'glob';

import { processTemplate } from '../../../utils/blueprints.js';
import { createFiles } from '../../../utils/files.js';
import { processTemplate } from '../../../utils/process-template.js';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
Expand Down
10 changes: 2 additions & 8 deletions src/migration/ember-addon/steps/move-addon-files.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import glob from 'glob';

import { moveFiles, removeFiles } from '../../../utils/files.js';
import { mapFilePaths } from '../../../utils/map-file-paths.js';
import { mapFilePaths, moveFiles, removeFiles } from '../../../utils/files.js';

function moveAddonFolder(options) {
const { locations, projectRoot } = options;
Expand Down Expand Up @@ -63,12 +62,7 @@ function removeAppFolder(options) {
nodir: true,
});

const pathMapping = mapFilePaths(filePaths, {
from: 'app',
to: 'app',
});

removeFiles(pathMapping, options);
removeFiles(filePaths, options);
}

export function moveAddonFiles(options) {
Expand Down
22 changes: 8 additions & 14 deletions src/migration/ember-addon/steps/move-project-root-files.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import glob from 'glob';

import { copyFiles, moveFiles, removeFiles } from '../../../utils/files.js';
import { mapFilePaths } from '../../../utils/map-file-paths.js';
import {
copyFiles,
mapFilePaths,
moveFiles,
removeFiles,
} from '../../../utils/files.js';

function globPattern(files) {
if (files.length <= 1) {
Expand Down Expand Up @@ -72,12 +76,7 @@ function moveToAddonAndTestApp(options) {

copyFiles(pathMapping, options);

pathMapping = mapFilePaths(filePaths, {
from: '',
to: '',
});

removeFiles(pathMapping, options);
removeFiles(filePaths, options);
}

function moveToTestApp(options) {
Expand Down Expand Up @@ -111,12 +110,7 @@ function removeFromProjectRoot(options) {
cwd: projectRoot,
});

const pathMapping = mapFilePaths(filePaths, {
from: '',
to: '',
});

removeFiles(pathMapping, options);
removeFiles(filePaths, options);
}

export function moveProjectRootFiles(options) {
Expand Down
3 changes: 1 addition & 2 deletions src/migration/ember-addon/steps/move-test-app-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { join } from 'node:path';

import glob from 'glob';

import { moveFiles } from '../../../utils/files.js';
import { mapFilePaths } from '../../../utils/map-file-paths.js';
import { mapFilePaths, moveFiles } from '../../../utils/files.js';

function moveTestsFolder(options) {
const { locations, projectRoot } = options;
Expand Down
7 changes: 2 additions & 5 deletions src/migration/ember-addon/steps/update-addon-package-json.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { readFileSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';

import {
convertToMap,
convertToObject,
} from '../../../utils/convert-json-object.js';
import { decideVersion } from '../../../utils/decide-version.js';
import { decideVersion } from '../../../utils/blueprints.js';
import { convertToMap, convertToObject } from '../../../utils/json.js';

function updateDependencies(packageJson, options) {
const dependencies = convertToMap(packageJson['dependencies']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
convertToMap,
convertToObject,
sanitizeJson,
} from '../../../utils/convert-json-object.js';
} from '../../../utils/json.js';

function updateCompilerOptions(tsconfigJson) {
const compilerOptions = convertToMap(tsconfigJson['compilerOptions']);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { readFileSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';

import {
convertToMap,
convertToObject,
} from '../../../utils/convert-json-object.js';
import { decideVersion } from '../../../utils/decide-version.js';
import { decideVersion } from '../../../utils/blueprints.js';
import { convertToMap, convertToObject } from '../../../utils/json.js';

function moveDependenciesToDevDependencies(packageJson, options) {
const { packages } = options;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
convertToMap,
convertToObject,
sanitizeJson,
} from '../../../utils/convert-json-object.js';
} from '../../../utils/json.js';

function updateCompilerOptions(tsconfigJson, options) {
const { packages } = options;
Expand Down
2 changes: 2 additions & 0 deletions src/utils/blueprints.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './blueprints/decide-version.js';
export * from './blueprints/process-template.js';
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* https://github.com/ember-cli/ember-cli/blob/v4.9.2/lib/utilities/process-template.js */
import template from 'lodash.template';

export function processTemplate(content, context) {
const options = {
export function processTemplate(file, data) {
const settings = {
Comment on lines +4 to +5
Copy link
Owner Author

@ijlee2 ijlee2 Jan 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I renamed the variables so that the words context and options don't carry more than one meaning.

escape: /<%-([\s\S]+?)%>/g,
evaluate: /<%([\s\S]+?)%>/g,
interpolate: /<%=([\s\S]+?)%>/g,
};

return template(content, options)(context);
return template(file, settings)(data);
}
14 changes: 0 additions & 14 deletions src/utils/convert-json-object.js

This file was deleted.

94 changes: 6 additions & 88 deletions src/utils/files.js
Original file line number Diff line number Diff line change
@@ -1,88 +1,6 @@
import {
copyFileSync,
existsSync,
mkdirSync,
readdirSync,
renameSync,
rmSync,
writeFileSync,
} from 'node:fs';
import { dirname, join } from 'node:path';

function removeDirectoryIfEmpty({ oldPath, projectRoot }) {
const directories = dirname(oldPath).split('/');
const depth = directories.length;

for (let i = 0; i < depth; i++) {
const directory = join(projectRoot, ...directories);
const numFilesLeft = readdirSync(directory).length;

if (numFilesLeft > 0) {
continue;
}

rmSync(directory, { recursive: true });
directories.pop();
}
}

export function copyFiles(pathMapping, options) {
const { projectRoot } = options;

pathMapping.forEach((newPath, oldPath) => {
const oldAbsolutePath = join(projectRoot, oldPath);

const newAbsolutePath = join(projectRoot, newPath);
const newDirectory = dirname(newAbsolutePath);

if (!existsSync(newDirectory)) {
mkdirSync(newDirectory, { recursive: true });
}

copyFileSync(oldAbsolutePath, newAbsolutePath);
});
}

export function createFiles(fileMapping, options) {
const { projectRoot } = options;

fileMapping.forEach((file, newPath) => {
const newAbsolutePath = join(projectRoot, newPath);
const newDirectory = dirname(newAbsolutePath);

if (!existsSync(newDirectory)) {
mkdirSync(newDirectory, { recursive: true });
}

writeFileSync(newAbsolutePath, file, 'utf8');
});
}

export function moveFiles(pathMapping, options) {
const { projectRoot } = options;

pathMapping.forEach((newPath, oldPath) => {
const oldAbsolutePath = join(projectRoot, oldPath);

const newAbsolutePath = join(projectRoot, newPath);
const newDirectory = dirname(newAbsolutePath);

if (!existsSync(newDirectory)) {
mkdirSync(newDirectory, { recursive: true });
}

renameSync(oldAbsolutePath, newAbsolutePath);
removeDirectoryIfEmpty({ oldPath, projectRoot });
});
}

export function removeFiles(pathMapping, options) {
const { projectRoot } = options;

pathMapping.forEach((newPath, oldPath) => {
const oldAbsolutePath = join(projectRoot, oldPath);

rmSync(oldAbsolutePath);
removeDirectoryIfEmpty({ oldPath, projectRoot });
});
}
export * from './files/copy-files.js';
export * from './files/create-files.js';
export * from './files/map-file-paths.js';
export * from './files/move-files.js';
export * from './files/remove-directory-if-empty.js';
export * from './files/remove-files.js';
19 changes: 19 additions & 0 deletions src/utils/files/copy-files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { copyFileSync, existsSync, mkdirSync } from 'node:fs';
import { dirname, join } from 'node:path';

export function copyFiles(pathMapping, options) {
const { projectRoot } = options;

pathMapping.forEach((newPath, oldPath) => {
const oldAbsolutePath = join(projectRoot, oldPath);

const newAbsolutePath = join(projectRoot, newPath);
const newDirectory = dirname(newAbsolutePath);

if (!existsSync(newDirectory)) {
mkdirSync(newDirectory, { recursive: true });
}

copyFileSync(oldAbsolutePath, newAbsolutePath);
});
}
Loading