-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
put cache command under Base command class
- Loading branch information
Showing
5 changed files
with
126 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
/* @flow */ | ||
|
||
export default class BaseCommand { | ||
hasWrapper(): boolean { | ||
hasWrapper(flags: Object, args: Array<string>): boolean { | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* @flow */ | ||
|
||
import BaseCommand from './_base.js'; | ||
import type {Reporter} from '../../reporters/index.js'; | ||
import type Config from '../../config.js'; | ||
import type {CLIFunction} from '../../types.js'; | ||
import {MessageError} from '../../errors.js'; | ||
import {camelCase, hyphenate} from '../../util/misc.js'; | ||
|
||
type SubCommands = { | ||
[commandName: string]: CLIFunction, | ||
}; | ||
|
||
type Usage = Array<string>; | ||
|
||
export default class SubCommand extends BaseCommand { | ||
rootCommandName: string; | ||
subCommands: SubCommands; | ||
subCommandNames: Array<string>; | ||
usage: Usage; | ||
examples: Array<string>; | ||
|
||
constructor(rootCommandName: string, subCommands: SubCommands, usage?: Usage = []) { | ||
super(); | ||
this.rootCommandName = rootCommandName; | ||
this.subCommands = subCommands; | ||
this.subCommandNames = Object.keys(subCommands).map(hyphenate); | ||
this.usage = usage; | ||
this.examples = usage.map((cmd: string): string => { | ||
return `${rootCommandName} ${cmd}`; | ||
}); | ||
} | ||
|
||
setFlags(commander: Object) { | ||
commander.usage(`${this.rootCommandName} [${this.subCommandNames.join('|')}] [flags]`); | ||
} | ||
|
||
async run( | ||
config: Config, | ||
reporter: Reporter, | ||
flags: Object, | ||
args: Array<string>, | ||
): Promise<void> { | ||
const subName: ?string = camelCase(args.shift() || ''); | ||
if (subName && this.subCommands[subName]) { | ||
const command: CLIFunction = this.subCommands[subName]; | ||
const res = await command(config, reporter, flags, args); | ||
if (res !== false) { | ||
return Promise.resolve(); | ||
} | ||
} | ||
|
||
if (this.usage && this.usage.length) { | ||
reporter.error(`${reporter.lang('usage')}:`); | ||
for (const msg of this.usage) { | ||
reporter.error(`yarn ${this.rootCommandName} ${msg}`); | ||
} | ||
} | ||
return Promise.reject(new MessageError(reporter.lang('invalidCommand', this.subCommandNames.join(', ')))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,76 @@ | ||
/* @flow */ | ||
|
||
import SubCommand from './_sub-command.js'; | ||
import type {Reporter} from '../../reporters/index.js'; | ||
import type Config from '../../config.js'; | ||
import buildSubCommands from './_build-sub-commands.js'; | ||
import * as fs from '../../util/fs.js'; | ||
import {METADATA_FILENAME} from '../../constants'; | ||
|
||
const path = require('path'); | ||
|
||
export function hasWrapper(flags: Object, args: Array<string>): boolean { | ||
return args[0] !== 'dir'; | ||
} | ||
export default class CacheCommand extends SubCommand { | ||
constructor() { | ||
super('cache', { | ||
async ls( | ||
config: Config, | ||
reporter: Reporter, | ||
flags: Object, | ||
args: Array<string>, | ||
): Promise<void> { | ||
async function readCacheMetadata( | ||
parentDir = config.cacheFolder, | ||
metadataFile = METADATA_FILENAME, | ||
): Promise<Array<Array<string>>> { | ||
const folders = await fs.readdir(parentDir); | ||
const packagesMetadata = []; | ||
|
||
export const {run, setFlags} = buildSubCommands('cache', { | ||
async ls( | ||
config: Config, | ||
reporter: Reporter, | ||
flags: Object, | ||
args: Array<string>, | ||
): Promise<void> { | ||
async function readCacheMetadata( | ||
parentDir = config.cacheFolder, | ||
metadataFile = METADATA_FILENAME, | ||
): Promise<Array<Array<string>>> { | ||
const folders = await fs.readdir(parentDir); | ||
const packagesMetadata = []; | ||
for (const folder of folders) { | ||
if (folder[0] === '.') { | ||
continue; | ||
} | ||
|
||
for (const folder of folders) { | ||
if (folder[0] === '.') { | ||
continue; | ||
} | ||
const loc = path.join(config.cacheFolder, parentDir.replace(config.cacheFolder, ''), folder); | ||
// Check if this is a scoped package | ||
if (!(await fs.exists(path.join(loc, metadataFile)))) { | ||
// If so, recurrently read scoped packages metadata | ||
packagesMetadata.push(...await readCacheMetadata(loc)); | ||
} else { | ||
const {registry, package: manifest, remote} = await config.readPackageMetadata(loc); | ||
packagesMetadata.push([manifest.name, manifest.version, registry, (remote && remote.resolved) || '']); | ||
} | ||
} | ||
|
||
const loc = path.join(config.cacheFolder, parentDir.replace(config.cacheFolder, ''), folder); | ||
// Check if this is a scoped package | ||
if (!(await fs.exists(path.join(loc, metadataFile)))) { | ||
// If so, recurrently read scoped packages metadata | ||
packagesMetadata.push(...await readCacheMetadata(loc)); | ||
} else { | ||
const {registry, package: manifest, remote} = await config.readPackageMetadata(loc); | ||
packagesMetadata.push([manifest.name, manifest.version, registry, (remote && remote.resolved) || '']); | ||
return packagesMetadata; | ||
} | ||
} | ||
|
||
return packagesMetadata; | ||
} | ||
const body = await readCacheMetadata(); | ||
|
||
const body = await readCacheMetadata(); | ||
reporter.table(['Name', 'Version', 'Registry', 'Resolved'], body); | ||
}, | ||
|
||
reporter.table(['Name', 'Version', 'Registry', 'Resolved'], body); | ||
}, | ||
dir( | ||
config: Config, | ||
reporter: Reporter, | ||
) { | ||
reporter.log(config.cacheFolder); | ||
}, | ||
|
||
dir( | ||
config: Config, | ||
reporter: Reporter, | ||
) { | ||
reporter.log(config.cacheFolder); | ||
}, | ||
async clean( | ||
config: Config, | ||
reporter: Reporter, | ||
flags: Object, | ||
args: Array<string>, | ||
): Promise<void> { | ||
if (config.cacheFolder) { | ||
await fs.unlink(config._cacheRootFolder); | ||
await fs.mkdirp(config.cacheFolder); | ||
reporter.success(reporter.lang('clearedCache')); | ||
} | ||
}, | ||
}); | ||
} | ||
|
||
async clean( | ||
config: Config, | ||
reporter: Reporter, | ||
flags: Object, | ||
args: Array<string>, | ||
): Promise<void> { | ||
if (config.cacheFolder) { | ||
await fs.unlink(config._cacheRootFolder); | ||
await fs.mkdirp(config.cacheFolder); | ||
reporter.success(reporter.lang('clearedCache')); | ||
} | ||
}, | ||
}); | ||
hasWrapper(flags: Object, args: Array<string>): boolean { | ||
return args[0] !== 'dir'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters