Skip to content

Commit

Permalink
chore: code review
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Jun 21, 2024
1 parent 2771b05 commit 050b2bb
Show file tree
Hide file tree
Showing 16 changed files with 490 additions and 76 deletions.
4 changes: 2 additions & 2 deletions src/generators/analyticsTemplateGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import * as path from 'path';
import { nls } from '../i18n';
import { CreateUtil } from '../utils';
import { AnalyticsTemplateOptions } from '../utils/types';
import { SfGenerator } from './sfGenerator';
import { BaseGenerator } from './baseGenerator';

export default class AnalyticsTemplateGenerator extends SfGenerator<AnalyticsTemplateOptions> {
export default class AnalyticsTemplateGenerator extends BaseGenerator<AnalyticsTemplateOptions> {
constructor(options: AnalyticsTemplateOptions) {
super(options);
}
Expand Down
4 changes: 2 additions & 2 deletions src/generators/apexClassGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import * as path from 'path';
import { CreateUtil } from '../utils';
import { ApexClassOptions } from '../utils/types';
import { SfGenerator } from './sfGenerator';
import { BaseGenerator } from './baseGenerator';

export default class ApexClassGenerator extends SfGenerator<ApexClassOptions> {
export default class ApexClassGenerator extends BaseGenerator<ApexClassOptions> {
constructor(options: ApexClassOptions) {
super(options);
}
Expand Down
4 changes: 2 additions & 2 deletions src/generators/apexTriggerGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import * as path from 'path';
import { CreateUtil } from '../utils';
import { ApexTriggerOptions } from '../utils/types';
import { SfGenerator } from './sfGenerator';
import { BaseGenerator } from './baseGenerator';

export default class ApexTriggerGenerator extends SfGenerator<ApexTriggerOptions> {
export default class ApexTriggerGenerator extends BaseGenerator<ApexTriggerOptions> {
constructor(options: ApexTriggerOptions) {
super(options);
}
Expand Down
16 changes: 4 additions & 12 deletions src/generators/sfGenerator.ts → src/generators/baseGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,17 @@
*/

import * as fs from 'fs';
import { access, mkdir, writeFile, readFile } from 'node:fs/promises';
import { mkdir, writeFile, readFile } from 'node:fs/promises';
import * as path from 'path';
import { CreateOutput, TemplateOptions } from '../utils/types';
import { renderFile } from 'ejs';
import { nls } from '../i18n';
import { loadCustomTemplatesGitRepo } from '../service/gitRepoUtils';

async function pathExists(path: string): Promise<boolean> {
return access(path)
.then(() => true)
.catch(() => false);
}

async function outputFile(file: string, data: string): Promise<void> {
const dir = path.dirname(file);

if (!(await pathExists(dir))) {
await mkdir(dir, { recursive: true });
}
await mkdir(dir, { recursive: true });

return writeFile(file, data);
}
Expand Down Expand Up @@ -74,7 +66,7 @@ export async function setCustomTemplatesRootPathOrGitRepo(
/**
* Look up package version of @salesforce/templates package to supply a default API version
*/
function getDefaultApiVersion(): string {
export function getDefaultApiVersion(): string {
const packageJsonPath = path.join('..', '..', 'package.json');
const versionTrimmed = require(packageJsonPath).salesforceApiVersion.trim();
return `${versionTrimmed.split('.')[0]}.0`;
Expand Down Expand Up @@ -173,7 +165,7 @@ abstract class NotYeoman {
}
}

export abstract class SfGenerator<
export abstract class BaseGenerator<
TOptions extends TemplateOptions
> extends NotYeoman {
/**
Expand Down
4 changes: 2 additions & 2 deletions src/generators/lightningAppGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import * as path from 'path';
import { nls } from '../i18n';
import { CreateUtil } from '../utils';
import { LightningAppOptions } from '../utils/types';
import { SfGenerator } from './sfGenerator';
import { BaseGenerator } from './baseGenerator';

export default class LightningAppGenerator extends SfGenerator<LightningAppOptions> {
export default class LightningAppGenerator extends BaseGenerator<LightningAppOptions> {
constructor(options: LightningAppOptions) {
super(options);
}
Expand Down
4 changes: 2 additions & 2 deletions src/generators/lightningComponentGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import * as path from 'path';
import { nls } from '../i18n';
import { CreateUtil } from '../utils';
import { LightningComponentOptions } from '../utils/types';
import { SfGenerator } from './sfGenerator';
import { BaseGenerator } from './baseGenerator';

export default class LightningComponentGenerator extends SfGenerator<LightningComponentOptions> {
export default class LightningComponentGenerator extends BaseGenerator<LightningComponentOptions> {
constructor(options: LightningComponentOptions) {
super(options);
}
Expand Down
4 changes: 2 additions & 2 deletions src/generators/lightningEventGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import * as path from 'path';
import { nls } from '../i18n';
import { CreateUtil } from '../utils';
import { LightningEventOptions } from '../utils/types';
import { SfGenerator } from './sfGenerator';
import { BaseGenerator } from './baseGenerator';

export default class LightningEventGenerator extends SfGenerator<LightningEventOptions> {
export default class LightningEventGenerator extends BaseGenerator<LightningEventOptions> {
constructor(options: LightningEventOptions) {
super(options);
}
Expand Down
4 changes: 2 additions & 2 deletions src/generators/lightningInterfaceGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import * as path from 'path';
import { nls } from '../i18n';
import { CreateUtil } from '../utils';
import { LightningInterfaceOptions } from '../utils/types';
import { SfGenerator } from './sfGenerator';
import { BaseGenerator } from './baseGenerator';

export default class LightningInterfaceGenerator extends SfGenerator<LightningInterfaceOptions> {
export default class LightningInterfaceGenerator extends BaseGenerator<LightningInterfaceOptions> {
constructor(options: LightningInterfaceOptions) {
super(options);
}
Expand Down
4 changes: 2 additions & 2 deletions src/generators/lightningTestGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import * as path from 'path';
import { nls } from '../i18n';
import { CreateUtil } from '../utils';
import { LightningTestOptions } from '../utils/types';
import { SfGenerator } from './sfGenerator';
import { BaseGenerator } from './baseGenerator';

export default class LightningTestGenerator extends SfGenerator<LightningTestOptions> {
export default class LightningTestGenerator extends BaseGenerator<LightningTestOptions> {
constructor(options: LightningTestOptions) {
super(options);
}
Expand Down
27 changes: 9 additions & 18 deletions src/generators/projectGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import * as fs from 'fs';
import { readFile, writeFile } from 'node:fs/promises';
import { mkdir, readFile, writeFile } from 'node:fs/promises';
import * as path from 'path';
import { CreateUtil } from '../utils';
import { ProjectOptions } from '../utils/types';
import { SfGenerator } from './sfGenerator';
import { BaseGenerator } from './baseGenerator';

const GITIGNORE = 'gitignore';
const HUSKY_FOLDER = '.husky';
Expand Down Expand Up @@ -53,7 +53,7 @@ async function extendJSON(
await writeFile(filepath, newContent);
}

export default class ProjectGenerator extends SfGenerator<ProjectOptions> {
export default class ProjectGenerator extends BaseGenerator<ProjectOptions> {
constructor(options: ProjectOptions) {
super(options);
this.sourceRootWithPartialPath('project');
Expand Down Expand Up @@ -121,7 +121,7 @@ export default class ProjectGenerator extends SfGenerator<ProjectOptions> {
}

if (template === 'standard') {
makeEmptyFolders(folderlayout, standardfolderarray);
await makeEmptyFolders(folderlayout, standardfolderarray);

// Add Husky directory and hooks
this._createHuskyConfig(path.join(this.outputdir, projectname));
Expand Down Expand Up @@ -186,7 +186,7 @@ export default class ProjectGenerator extends SfGenerator<ProjectOptions> {
}

if (template === 'empty') {
makeEmptyFolders(folderlayout, emptyfolderarray);
await makeEmptyFolders(folderlayout, emptyfolderarray);
await this.render(
this.templatePath('.forceignore'),
this.destinationPath(
Expand All @@ -197,7 +197,7 @@ export default class ProjectGenerator extends SfGenerator<ProjectOptions> {
}

if (template === 'analytics') {
makeEmptyFolders(folderlayout, analyticsfolderarray);
await makeEmptyFolders(folderlayout, analyticsfolderarray);

// Add Husky directory and hooks
this._createHuskyConfig(path.join(this.outputdir, projectname));
Expand Down Expand Up @@ -265,20 +265,11 @@ export default class ProjectGenerator extends SfGenerator<ProjectOptions> {
}
}

function makeEmptyFolders(
async function makeEmptyFolders(
toplevelfolders: string[],
metadatafolders: string[]
) {
let oldfolder = '';
for (const folder of toplevelfolders) {
if (!fs.existsSync(path.join(oldfolder, folder))) {
fs.mkdirSync(path.join(oldfolder, folder));
}
oldfolder = path.join(oldfolder, folder);
}
for (const newfolder of metadatafolders) {
if (!fs.existsSync(path.join(oldfolder, newfolder))) {
fs.mkdirSync(path.join(oldfolder, newfolder));
}
for (const folder of metadatafolders) {
await mkdir(path.join(...toplevelfolders, folder), { recursive: true });
}
}
4 changes: 2 additions & 2 deletions src/generators/staticResourceGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import * as path from 'path';
import { nls } from '../i18n';
import { CreateUtil } from '../utils';
import { StaticResourceOptions } from '../utils/types';
import { SfGenerator } from './sfGenerator';
import { BaseGenerator } from './baseGenerator';

const EXTENSION_TEMPLATES = ['js', 'css', 'json', 'txt'];

export default class StaticResourceGenerator extends SfGenerator<StaticResourceOptions> {
export default class StaticResourceGenerator extends BaseGenerator<StaticResourceOptions> {
constructor(options: StaticResourceOptions) {
super(options);
}
Expand Down
4 changes: 2 additions & 2 deletions src/generators/visualforceComponentGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import * as path from 'path';
import { CreateUtil } from '../utils';
import { VisualforceComponentOptions } from '../utils/types';
import { SfGenerator } from './sfGenerator';
import { BaseGenerator } from './baseGenerator';

export default class VisualforceComponentGenerator extends SfGenerator<VisualforceComponentOptions> {
export default class VisualforceComponentGenerator extends BaseGenerator<VisualforceComponentOptions> {
constructor(options: VisualforceComponentOptions) {
super(options);
}
Expand Down
4 changes: 2 additions & 2 deletions src/generators/visualforcePageGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import * as path from 'path';
import { CreateUtil } from '../utils';
import { VisualforcePageOptions } from '../utils/types';
import { SfGenerator } from './sfGenerator';
import { BaseGenerator } from './baseGenerator';

export default class VisualforcePageGenerator extends SfGenerator<VisualforcePageOptions> {
export default class VisualforcePageGenerator extends BaseGenerator<VisualforcePageOptions> {
constructor(options: VisualforcePageOptions) {
super(options);
}
Expand Down
8 changes: 4 additions & 4 deletions src/service/templateService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
TemplateType,
} from '../utils/types';

async function importGenerator(templateType: TemplateType) {
export async function importGenerator(templateType: TemplateType) {
const generatorClass =
TemplateType[templateType].toString().charAt(0).toLowerCase() +
TemplateType[templateType].toString().slice(1) +
Expand All @@ -32,7 +32,7 @@ export class TemplateService {

/**
* Get an instance of TemplateService
* @param cwd cwd of current yeoman environment. CLI: don't need to set explicitly. VS Code: it's typically the root workspace path
* @param cwd cwd of current environment. CLI: don't need to set explicitly. VS Code: it's typically the root workspace path
*/
public static getInstance(cwd?: string): TemplateService {
if (!TemplateService.instance) {
Expand All @@ -44,14 +44,14 @@ export class TemplateService {
}

/**
* Getting cwd of current yeoman environment
* Getting cwd of current environment
*/
public get cwd(): string {
return this._cwd;
}

/**
* Setting cwd of current yeoman environment
* Setting cwd of current environment
* In VS Code, it's typically the root workspace path
*/
public set cwd(cwd: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,20 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import * as path from 'path';
import * as sinon from 'sinon';
import { expect } from 'chai';
import { TemplateOptions } from '../../src';
import { SfGenerator } from '../../src/generators/sfGenerator';
import {
BaseGenerator,
getDefaultApiVersion,
} from '../../src/generators/baseGenerator';

function getDefaultApiVersion(): string {
const packageJsonPath = path.join('..', '..', 'package.json');
const versionTrimmed = require(packageJsonPath).salesforceApiVersion.trim();
return `${versionTrimmed.split('.')[0]}.0`;
}

describe('SfGenerator', () => {
describe('BaseGenerator', () => {
const API_VERSION = getDefaultApiVersion();
interface MyTemplateOptions extends TemplateOptions {
customProp: boolean;
}
class MyGenerator extends SfGenerator<MyTemplateOptions> {
class MyGenerator extends BaseGenerator<MyTemplateOptions> {
public validateOptions() {}
public async generate() {
this.doWriting({
Expand Down Expand Up @@ -64,3 +60,10 @@ describe('SfGenerator', () => {
expect(validateOptionsStub.calledOnce).to.be.true;
});
});

describe('getDefaultApiVersion', () => {
it('should return the default api version', async () => {
const pjson = await import('../../package.json');
expect(getDefaultApiVersion()).to.equal(`${pjson.salesforceApiVersion}.0`);
});
});
Loading

0 comments on commit 050b2bb

Please sign in to comment.