Skip to content

Commit

Permalink
fix: edge case compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
neikvon committed Oct 13, 2020
1 parent b9dce77 commit 06bc029
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 20 deletions.
8 changes: 4 additions & 4 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export class Cli extends BaseClass {
if (isBuiltInCmd) {
this.registerCommands(this.fbi.commands)
} else {
const factoryId = (config.factory && config.factory.id) || ''
const factoryVersion = (config.factory && config.factory.version) || ''
const factoryId = config?.factory?.id || ''
const factoryVersion = config?.factory?.version || ''
const factories: Factory[] = this.fbi.resolveGlobalFactories()

if (factoryId) {
Expand All @@ -40,7 +40,7 @@ export class Cli extends BaseClass {
}

for (const factory of factories) {
if (factory.commands) {
if (factory?.commands) {
this.registerCommands(factory.commands)
}
}
Expand Down Expand Up @@ -136,7 +136,7 @@ export class Cli extends BaseClass {
})
.on('--help', () => {
console.log('')
if (command.examples && command.examples.length > 0) {
if (command.examples?.length > 0) {
console.log('Examples:')
for (const str of command.examples) {
console.log(` ${str}`)
Expand Down
2 changes: 1 addition & 1 deletion src/commands/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default class CommandAdd extends Command {
flags
})
const config = this.context.get('config')
const rootDir = join(config.rootDirectory, config.directoryName)
const rootDir = join(config?.rootDirectory, config?.directoryName)

for (const repo of repositories) {
const info = this.getBaseInfo(repo, config)
Expand Down
6 changes: 5 additions & 1 deletion src/commands/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ export default class CommandCreate extends Command {
const projectName = cwd.split(sep).pop()

// set init data
const factoryInfo = this.store.get(template.factory.id)
const factoryInfo = this.store.get(template?.factory?.id)
if (!factoryInfo) {
this.warn(`can not resolve factory info '${template?.factory?.id}'`)
this.exit()
}
const info: Record<string, any> = await template.run(
{
factory: {
Expand Down
3 changes: 0 additions & 3 deletions src/commands/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ export default class CommandLink extends Command {
})
const ids = (Array.isArray(factories) && factories.length > 0 && factories) || ['.']

const config = this.context.get('config')
const factoriesDir = join(config.rootDirectory, config.directoryName)

for (const id of ids) {
// base info
const factory = this.factory.createFactory(id)
Expand Down
6 changes: 4 additions & 2 deletions src/commands/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,12 @@ export default class CommandRemove extends Command {

private async deleteFiles(factory: any) {
// remove main dir
await this.fs.remove(factory.path)
if (factory?.path) {
await this.fs.remove(factory.path)
}

// remove version dirs
if (factory.version?.versions) {
if (factory?.version?.versions) {
for (let version of factory.version.versions) {
await this.fs.remove(join(factory.version.baseDir, `${factory.id}__${version.short}`))
}
Expand Down
18 changes: 9 additions & 9 deletions src/fbi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export class Fbi extends Factory {

// create from local
const local = this.context.get('config.factory')
if (local && local.id && !this.factories.find((f) => f.id === local.id)) {
if (local?.id && !this.factories.find((f) => f.id === local.id)) {
this.resolveFromLocal(local.id, local.version)
}

Expand Down Expand Up @@ -129,9 +129,9 @@ export class Fbi extends Factory {
const factories = this.store.get()
for (const [_, value] of Object.entries(factories)) {
const info: any = value
if (info.global) {
globalFactories.push(this.createFactory(info.path))
this.debug('Fbi<resolveGlobalFactories>:', info.id)
if (info?.global) {
globalFactories.push(this.createFactory(info?.path))
this.debug('Fbi<resolveGlobalFactories>:', info?.id)
}
}

Expand All @@ -142,7 +142,7 @@ export class Fbi extends Factory {
if (!targetId) {
return null
}
return this.commands.find((cmd) => cmd.id === targetId)
return this.commands.find((cmd) => cmd?.id === targetId)
}

public resolveTemplates(targetId?: string) {
Expand All @@ -157,7 +157,7 @@ export class Fbi extends Factory {
}

private resolveFromCache(targetId: string, targetVersion?: string) {
const factory = this.factories.find((f) => f.id === targetId)
const factory = this.factories.find((f) => f?.id === targetId)
if (!factory) {
return null
}
Expand All @@ -174,15 +174,15 @@ export class Fbi extends Factory {
this.debug(`Factory "${targetId}" found in store`)
// local link not support version control
if (targetVersion && factoryInfo.type !== 'local') {
const matchVersion = factoryInfo.version?.versions
? getMatchVersion(factoryInfo.version?.versions, targetVersion)
const matchVersion = factoryInfo?.version?.versions
? getMatchVersion(factoryInfo.version.versions, targetVersion)
: null
if (!matchVersion) {
return null
}

return this.createFactory(
getPathByVersion(factoryInfo.version?.baseDir as string, factoryInfo.id, matchVersion)
getPathByVersion(factoryInfo?.version?.baseDir as string, factoryInfo.id, matchVersion)
)
}
return this.createFactory(factoryInfo.path)
Expand Down

0 comments on commit 06bc029

Please sign in to comment.