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

Refactored blueprints-addon #19

Merged
merged 4 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/eleven-monkeys-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-v2-addon-repo": patch
---

Refactored blueprints-addon
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ function getPattern(options: Options): string {
}

export function removeTestFile(options: Options): void {
const testAppRoot = join(options.projectRoot, options.testApp.location);
const { projectRoot, testApp } = options;

const testAppRoot = join(projectRoot, testApp.location);

const filePaths = findFiles(getPattern(options), {
projectRoot: testAppRoot,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { join } from 'node:path';

import { classify, doubleColonize } from '@codemod-utils/ember-cli-string';
import { readPackageJson } from '@codemod-utils/json';

import type { CodemodOptions, Options } from '../../types/run-generate.js';

function getAddonName(codemodOptions: CodemodOptions): string {
const { projectRoot } = codemodOptions;

const packageJson = readPackageJson({
projectRoot,
});
function getPackageName(projectRoot: string): string {
const packageJson = readPackageJson({ projectRoot });

return packageJson['name']!;
}
Expand All @@ -22,7 +20,7 @@ export function createOptions(codemodOptions: CodemodOptions): Options {

return {
addon: {
name: getAddonName(codemodOptions),
name: getPackageName(projectRoot),
},
entity: {
...entity,
Expand All @@ -33,7 +31,7 @@ export function createOptions(codemodOptions: CodemodOptions): Options {
projectRoot,
testApp: {
location: testAppLocation,
name: 'test-app',
name: getPackageName(join(projectRoot, testAppLocation)),
useTemplateTag: true,
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ import { createFiles, findFiles, parseFilePath } from '@codemod-utils/files';
import type { Options } from '../../../types/run-generate.js';
import { blueprintsRoot } from '../../../utils/blueprints.js';

function getData(options: Options) {
const { name: localFileName } = parseFilePath(options.entity.name);
function getLocalFileName(entityName: string): string {
const { name } = parseFilePath(entityName);

return {
localFileName,
};
return name;
}

function resolveBlueprintFilePath(
Expand All @@ -28,25 +26,31 @@ function resolveBlueprintFilePath(
}

export function createEntity(options: Options): void {
const { entity } = options;

const cwd = join(
blueprintsRoot,
'run-generate',
options.entity.type,
options.entity.blueprint,
entity.type,
entity.blueprint,
);

const blueprintFilePaths = findFiles('**/*', {
projectRoot: cwd,
});

const data = {
localFileName: getLocalFileName(entity.name),
};

const fileMap = new Map(
blueprintFilePaths.map((blueprintFilePath) => {
const filePath = resolveBlueprintFilePath(blueprintFilePath, options);

const blueprintFile = readFileSync(join(cwd, blueprintFilePath), 'utf8');

const file = processTemplate(blueprintFile, {
data: getData(options),
data,
options,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,14 @@ function resolveBlueprintFilePath(
}

export function createTestFile(options: Options): void {
const { entity, projectRoot, testApp } = options;

const filesToSkip = getFilesToSkip(options);

const cwd = join(
blueprintsRoot,
'run-generate',
options.entity.type,
entity.type,
'__testAppLocation__',
);

Expand All @@ -85,6 +87,6 @@ export function createTestFile(options: Options): void {
);

createFiles(fileMap, {
projectRoot: join(options.projectRoot, options.testApp.location),
projectRoot: join(projectRoot, testApp.location),
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function pascalCase(packageName: string): string {

return dasherizedName
.split('-')
.map((token) => token.charAt(0).toUpperCase() + token.substring(1))
.map((token) => `${token.charAt(0).toUpperCase()}${token.substring(1)}`)
.join('');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ function getPattern(options: Options): string {
}

export function removeTestFile(options: Options): void {
const testAppRoot = join(options.projectRoot, options.testApp.location);
const { projectRoot, testApp } = options;

const testAppRoot = join(projectRoot, testApp.location);

const filePaths = findFiles(getPattern(options), {
projectRoot: testAppRoot,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { join } from 'node:path';

import { classify, doubleColonize } from '@codemod-utils/ember-cli-string';
import { readPackageJson } from '@codemod-utils/json';

import type { CodemodOptions, Options } from '../../types/run-generate.js';

function getAddonName(codemodOptions: CodemodOptions): string {
const { projectRoot } = codemodOptions;

const packageJson = readPackageJson({
projectRoot,
});
function getPackageName(projectRoot: string): string {
const packageJson = readPackageJson({ projectRoot });

return packageJson['name']!;
}
Expand All @@ -22,7 +20,7 @@ export function createOptions(codemodOptions: CodemodOptions): Options {

return {
addon: {
name: getAddonName(codemodOptions),
name: getPackageName(projectRoot),
},
entity: {
...entity,
Expand All @@ -33,7 +31,7 @@ export function createOptions(codemodOptions: CodemodOptions): Options {
projectRoot,
testApp: {
location: testAppLocation,
name: 'test-app',
name: getPackageName(join(projectRoot, testAppLocation)),
useTemplateTag: true,
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ import { createFiles, findFiles, parseFilePath } from '@codemod-utils/files';
import type { Options } from '../../../types/run-generate.js';
import { blueprintsRoot } from '../../../utils/blueprints.js';

function getData(options: Options) {
const { name: localFileName } = parseFilePath(options.entity.name);
function getLocalFileName(entityName: string): string {
const { name } = parseFilePath(entityName);

return {
localFileName,
};
return name;
}

function resolveBlueprintFilePath(
Expand All @@ -28,25 +26,31 @@ function resolveBlueprintFilePath(
}

export function createEntity(options: Options): void {
const { entity } = options;

const cwd = join(
blueprintsRoot,
'run-generate',
options.entity.type,
options.entity.blueprint,
entity.type,
entity.blueprint,
);

const blueprintFilePaths = findFiles('**/*', {
projectRoot: cwd,
});

const data = {
localFileName: getLocalFileName(entity.name),
};

const fileMap = new Map(
blueprintFilePaths.map((blueprintFilePath) => {
const filePath = resolveBlueprintFilePath(blueprintFilePath, options);

const blueprintFile = readFileSync(join(cwd, blueprintFilePath), 'utf8');

const file = processTemplate(blueprintFile, {
data: getData(options),
data,
options,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,14 @@ function resolveBlueprintFilePath(
}

export function createTestFile(options: Options): void {
const { entity, projectRoot, testApp } = options;

const filesToSkip = getFilesToSkip(options);

const cwd = join(
blueprintsRoot,
'run-generate',
options.entity.type,
entity.type,
'__testAppLocation__',
);

Expand All @@ -85,6 +87,6 @@ export function createTestFile(options: Options): void {
);

createFiles(fileMap, {
projectRoot: join(options.projectRoot, options.testApp.location),
projectRoot: join(projectRoot, testApp.location),
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function pascalCase(packageName: string): string {

return dasherizedName
.split('-')
.map((token) => token.charAt(0).toUpperCase() + token.substring(1))
.map((token) => `${token.charAt(0).toUpperCase()}${token.substring(1)}`)
.join('');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ function getPattern(options: Options): string {
}

export function removeTestFile(options: Options): void {
const testAppRoot = join(options.projectRoot, options.testApp.location);
const { projectRoot, testApp } = options;

const testAppRoot = join(projectRoot, testApp.location);

const filePaths = findFiles(getPattern(options), {
projectRoot: testAppRoot,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { join } from 'node:path';

import { classify, doubleColonize } from '@codemod-utils/ember-cli-string';
import { readPackageJson } from '@codemod-utils/json';

import type { CodemodOptions, Options } from '../../types/run-generate.js';

function getAddonName(codemodOptions: CodemodOptions): string {
const { projectRoot } = codemodOptions;

const packageJson = readPackageJson({
projectRoot,
});
function getPackageName(projectRoot: string): string {
const packageJson = readPackageJson({ projectRoot });

return packageJson['name']!;
}
Expand All @@ -22,7 +20,7 @@ export function createOptions(codemodOptions: CodemodOptions): Options {

return {
addon: {
name: getAddonName(codemodOptions),
name: getPackageName(projectRoot),
},
entity: {
...entity,
Expand All @@ -33,7 +31,7 @@ export function createOptions(codemodOptions: CodemodOptions): Options {
projectRoot,
testApp: {
location: testAppLocation,
name: 'test-app',
name: getPackageName(join(projectRoot, testAppLocation)),
useTemplateTag: true,
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ import { createFiles, findFiles, parseFilePath } from '@codemod-utils/files';
import type { Options } from '../../../types/run-generate.js';
import { blueprintsRoot } from '../../../utils/blueprints.js';

function getData(options: Options) {
const { name: localFileName } = parseFilePath(options.entity.name);
function getLocalFileName(entityName: string): string {
const { name } = parseFilePath(entityName);

return {
localFileName,
};
return name;
}

function resolveBlueprintFilePath(
Expand All @@ -28,25 +26,31 @@ function resolveBlueprintFilePath(
}

export function createEntity(options: Options): void {
const { entity } = options;

const cwd = join(
blueprintsRoot,
'run-generate',
options.entity.type,
options.entity.blueprint,
entity.type,
entity.blueprint,
);

const blueprintFilePaths = findFiles('**/*', {
projectRoot: cwd,
});

const data = {
localFileName: getLocalFileName(entity.name),
};

const fileMap = new Map(
blueprintFilePaths.map((blueprintFilePath) => {
const filePath = resolveBlueprintFilePath(blueprintFilePath, options);

const blueprintFile = readFileSync(join(cwd, blueprintFilePath), 'utf8');

const file = processTemplate(blueprintFile, {
data: getData(options),
data,
options,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,14 @@ function resolveBlueprintFilePath(
}

export function createTestFile(options: Options): void {
const { entity, projectRoot, testApp } = options;

const filesToSkip = getFilesToSkip(options);

const cwd = join(
blueprintsRoot,
'run-generate',
options.entity.type,
entity.type,
'__testAppLocation__',
);

Expand All @@ -85,6 +87,6 @@ export function createTestFile(options: Options): void {
);

createFiles(fileMap, {
projectRoot: join(options.projectRoot, options.testApp.location),
projectRoot: join(projectRoot, testApp.location),
});
}
Loading