-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkdir.ts
83 lines (67 loc) · 2.36 KB
/
mkdir.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import {Args, Command, Flags} from '@oclif/core'
import path from 'node:path'
import * as KeClient from '../client.js'
import {checkCwd, checkInsecure} from '../common.js'
import Config from '../config.js'
import {DIRECTORY_TYPE} from '../const.js'
import {common as commonFlags} from '../flags.js'
type MkdirOptions = {
parent?: boolean
verbose?: boolean
}
export default class Mkdir extends Command {
static override args = {
directory: Args.string({
description: 'path of the directory to be created',
required: true,
}),
}
static override description = 'Create the directories, if they do not already exist.'
static override examples = [
'<%= config.bin %> <%= command.id %>',
]
static override flags = {
...commonFlags,
// flag with a value (-c, --cwd=VALUE)
cwd: Flags.string({char: 'c', description: 'set current working directory to VALUE'}),
parent: Flags.boolean({char: 'p', description: 'no error if existing, make parent directories as needed'}),
verbose: Flags.boolean({char: 'v', description: 'print a message for each created directory'}),
}
static strict = false
private conf!: Config
public async run(): Promise<void> {
const {argv, flags} = await this.parse(Mkdir)
this.conf = new Config(flags)
checkInsecure(flags.insecure)
const cwd = await checkCwd(this.conf, flags.cwd)
await Promise.all(
argv.map((arg) => {
const {base, dir} = path.parse(path.resolve(cwd, arg as string))
return this.makeDir(dir, base, flags)
})
)
}
private async makeDir(parentDir: string, name: string, options: MkdirOptions) {
// parentDir の存在チェック
let result = await KeClient.get(this.conf, parentDir)
if (result === null) {
if (!options.parent) {
this.error(`cannot create directory: parent directory '${parentDir}' is not found`)
}
const {base, dir} = path.parse(parentDir)
result = await this.makeDir(dir, base, options)
}
if (result!.type_object !== DIRECTORY_TYPE) {
this.error(`cannot create directory: parent directory '${parentDir}' is not directory`)
}
const data = {
name,
'type_object': DIRECTORY_TYPE,
}
result = await KeClient.create(this.conf, parentDir, data)
if (result && options.verbose) {
this.log(`created directory: ${result.abspath}`)
}
return result
}
}