Skip to content

Commit

Permalink
fix: create command adaptions (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
dennismeister93 authored Oct 13, 2023
1 parent b118d43 commit d641b5b
Show file tree
Hide file tree
Showing 14 changed files with 143 additions and 93 deletions.
3 changes: 1 addition & 2 deletions src/commands/cache/clear.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ export default class Clear extends Command {

// although we are not reading the project config, we want to
// ensure the command is run in a project directory only.
ProjectConfig.read();

ProjectConfig.read(`v${this.config.version}`);
const cache = ProjectCache.read();
cache.clear();
cache.write();
Expand Down
2 changes: 1 addition & 1 deletion src/commands/cache/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ bar`,

// although we are not reading the project config, we want to
// ensure the command is run in a project directory only.
ProjectConfig.read();
ProjectConfig.read(`v${this.config.version}`);

const cache = ProjectCache.read();

Expand Down
2 changes: 1 addition & 1 deletion src/commands/cache/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default class Set extends Command {

// although we are not reading the project config, we want to
// ensure the command is run in a project directory only.
ProjectConfig.read();
ProjectConfig.read(`v${this.config.version}`);

const cache = ProjectCache.read();
cache.set(args.key, args.value);
Expand Down
69 changes: 30 additions & 39 deletions src/commands/create/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ export default class Create extends Command {
name: arg.id,
prefix: '',
message: `Config '${arg.id}' for interface '${interfaceEntry}': ${arg.description}`,
default: arg.default,
validate: (input: any) => {
if (!input) {
console.log('No empty value allowed for required argument!');
return false;
} else {
return true;
}
},
type: 'input',
};
},
Expand All @@ -117,27 +126,23 @@ export default class Create extends Command {
}

private async _runInteractiveMode(flags: any) {
let interactiveResponses: any = await inquirer.prompt([
Create.prompts.name,
Create.prompts.language,
Create.prompts.exampleQuestion,
]);
const interactiveResponses: any = await inquirer.prompt([Create.prompts.language, Create.prompts.exampleQuestion]);

flags.name = interactiveResponses.name;
flags.language = interactiveResponses.language;
flags.example = interactiveResponses.exampleQuestion;

if (flags.example) {
availableExamples = this._filterAvailableExamplesByLanguage(flags.language);
interactiveResponses = await inquirer.prompt([Create.prompts.exampleUse]);
const exampleResponse = await inquirer.prompt([Create.prompts.exampleUse]);
flags.example = flags.name = exampleResponse.exampleUse;
} else {
interactiveResponses = await inquirer.prompt([Create.prompts.interface]);
}
flags.example = interactiveResponses.exampleUse;
flags.interface = interactiveResponses.interface;
const interactiveSkeletonAppResponses: any = await inquirer.prompt([Create.prompts.name, Create.prompts.interface]);
flags.name = interactiveSkeletonAppResponses.name;
flags.interface = interactiveSkeletonAppResponses.interface;

if (flags.interface && flags.interface.length > 0) {
await this._handleAdditionalInterfaceArgs(flags.interface);
if (flags.interface && flags.interface.length > 0) {
await this._handleAdditionalInterfaceArgs(flags.interface);
}
}
}

Expand Down Expand Up @@ -185,30 +190,6 @@ export default class Create extends Command {
});
}

private async _setDefaultAppManifestInterfaceConfig(interfaces: string[]) {
if (this.appManifestInterfaces.interfaces.length > 0) {
return;
}
const interfacesToUse =
Array.isArray(interfaces) && interfaces.length
? availableInterfaces.filter((interfaceEntry) => interfaces.includes(interfaceEntry.value))
: availableInterfaces;

for (const interfaceEntry of interfacesToUse) {
const defaultAppManifestInterfaceConfig: AppManifestInterfaceEntry = {
type: interfaceEntry.value,
config: {},
};

for (const arg of interfaceEntry.args) {
defaultAppManifestInterfaceConfig.config[arg.id] =
arg.type === 'object' && arg.default ? JSON.parse(arg.default) : arg.default;
}

this.appManifestInterfaces.interfaces.push(defaultAppManifestInterfaceConfig);
}
}

private _getScriptExecutionPath(sdkConfig: SdkConfig): string {
const basePath = process.env.VELOCITAS_SDK_PATH_OVERRIDE
? process.env.VELOCITAS_SDK_PATH_OVERRIDE
Expand All @@ -223,6 +204,14 @@ export default class Create extends Command {
const { flags } = await this.parse(Create);
this.log(`Creating a new Velocitas project ...`);

if (flags.name && flags.example) {
throw new Error("Flags 'name' and 'example' are mutually exclusive!");
}

if (flags.example) {
flags.name = flags.example;
}

if (Object.keys(flags).length === 0) {
this.log('Interactive project creation started');
await this._runInteractiveMode(flags);
Expand All @@ -235,8 +224,10 @@ export default class Create extends Command {
throw new Error("Missing required flag 'language'");
}

if (!flags.example) {
this._setDefaultAppManifestInterfaceConfig(flags.interface!);
if (!flags.example && flags.interface) {
if (this.appManifestInterfaces.interfaces.length === 0 && flags.interface.length > 0) {
await this._handleAdditionalInterfaceArgs(flags.interface);
}
}

await ProjectConfig.create(packageIndex.getExtensions(), flags.language, this.config.version);
Expand Down
2 changes: 1 addition & 1 deletion src/commands/exec/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ Executing script...
const programArgsAndFlags = this._extractProgramArgsAndFlags();
const { args, flags } = await this.parse(Exec);

const projectConfig = ProjectConfig.read();
const projectConfig = ProjectConfig.read(`v${this.config.version}`);

const execSpec: ExecSpec = {
ref: args.ref,
Expand Down
4 changes: 2 additions & 2 deletions src/commands/init/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ Velocitas project found!

if (!ProjectConfig.isAvailable()) {
this.log('... Directory is no velocitas project. Creating .velocitas.json at the root of your repository.');
projectConfig = new ProjectConfig();
projectConfig = new ProjectConfig(`v${this.config.version}`);
projectConfig.write();
}
projectConfig = ProjectConfig.read();
projectConfig = ProjectConfig.read(`v${this.config.version}`);

for (const packageConfig of projectConfig.packages) {
if (!flags.force && packageConfig.isPackageInstalled()) {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/package/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ $ velocitas component --get-path devenv-runtime-local
async run(): Promise<void> {
const { args, flags } = await this.parse(Package);

const projectConfig = ProjectConfig.read();
const projectConfig = ProjectConfig.read(`v${this.config.version}`);

let packagesToPrint: Array<PackageConfig>;

Expand Down
2 changes: 1 addition & 1 deletion src/commands/sync/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Syncing Velocitas components!

async run(): Promise<void> {
this.log(`Syncing Velocitas components!`);
const projectConfig = ProjectConfig.read();
const projectConfig = ProjectConfig.read(`v${this.config.version}`);
const setupComponents = findComponentsByType(projectConfig, ComponentType.setup);
for (const setupComponent of setupComponents) {
this.log(`... syncing '${setupComponent[0].getPackageName()}'`);
Expand Down
6 changes: 5 additions & 1 deletion src/commands/upgrade/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Checking for updates!
const { flags } = await this.parse(Upgrade);

this.log(`Checking for updates!`);
const projectConfig = ProjectConfig.read();
const projectConfig = ProjectConfig.read(`v${this.config.version}`);
for (const packageConfig of projectConfig.packages) {
const availableVersions = await packageConfig.getPackageVersions();
try {
Expand Down Expand Up @@ -75,5 +75,9 @@ Checking for updates!
this.error(`Error during upgrade: '${e}'`);
}
}
if (!projectConfig.cliVersion) {
projectConfig.cliVersion = `v${this.config.version}`;
projectConfig.write();
}
}
}
13 changes: 7 additions & 6 deletions src/modules/project-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const DEFAULT_CONFIG_FILE_PATH = resolve(cwd(), './.velocitas.json');
interface ProjectConfigOptions {
packages: PackageConfig[];
variables: Map<string, any>;
cliVersion?: string;
}

export class ProjectConfig implements ProjectConfigOptions {
Expand All @@ -35,7 +36,7 @@ export class ProjectConfig implements ProjectConfigOptions {

// project-wide variable configuration
variables: Map<string, any> = new Map<string, any>();
cliVersion: string | undefined;
cliVersion: string;

private static _parsePackageConfig(packages: PackageConfig[]): PackageConfig[] {
const configArray: PackageConfig[] = [];
Expand All @@ -45,16 +46,17 @@ export class ProjectConfig implements ProjectConfigOptions {
return configArray;
}

constructor(config?: ProjectConfigOptions) {
constructor(cliVersion: string, config?: ProjectConfigOptions) {
this.packages = config?.packages ? ProjectConfig._parsePackageConfig(config.packages) : this.packages;
this.variables = config?.variables ? config.variables : this.variables;
this.cliVersion = config?.cliVersion ? config.cliVersion : cliVersion;
}

static read(path: PathLike = DEFAULT_CONFIG_FILE_PATH): ProjectConfig {
static read(cliVersion: string, path: PathLike = DEFAULT_CONFIG_FILE_PATH): ProjectConfig {
let config: ProjectConfig;

try {
config = new ProjectConfig(JSON.parse(readFileSync(path, DEFAULT_BUFFER_ENCODING)));
config = new ProjectConfig(cliVersion, JSON.parse(readFileSync(path, DEFAULT_BUFFER_ENCODING)));
} catch (error) {
throw new Error(`Error in parsing .velocitas.json: ${(error as Error).message}`);
}
Expand Down Expand Up @@ -83,7 +85,7 @@ export class ProjectConfig implements ProjectConfigOptions {
static isAvailable = (path: PathLike = DEFAULT_CONFIG_FILE_PATH) => existsSync(path);

static async create(usedExtensions: PkgIndexEntry[], language: string, cliVersion: string) {
const projectConfig = new ProjectConfig();
const projectConfig = new ProjectConfig(`v${cliVersion}`);
for (const extension of usedExtensions) {
const packageConfig = new PackageConfig({ name: extension.package });
const versions = await packageConfig.getPackageVersions();
Expand All @@ -97,7 +99,6 @@ export class ProjectConfig implements ProjectConfigOptions {
projectConfig.variables.set('repoType', 'app');
projectConfig.variables.set('appManifestPath', DEFAULT_APP_MANIFEST_PATH);
projectConfig.variables.set('githubRepoId', '<myrepo>');
projectConfig.cliVersion = cliVersion;
projectConfig.write();
}

Expand Down
Loading

0 comments on commit d641b5b

Please sign in to comment.