• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

rokucommunity / ropm / #874

23 Jan 2024 06:13PM UTC coverage: 89.291%. Remained the same
#874

push

GitHub
Merge 741ad53e2 into 803e66631

517 of 626 branches covered (0.0%)

Branch coverage included in aggregate %.

642 of 672 relevant lines covered (95.54%)

66.73 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.54
/src/commands/InitCommand.ts
1
import * as path from 'path';
1✔
2
import type { RopmPackageJson } from '../util';
3
import { util } from '../util';
1✔
4
import * as fsExtra from 'fs-extra';
1✔
5

6
export class InitCommand {
1✔
7
    constructor(
8
        public args: InitCommandArgs
5✔
9
    ) {
10

11
    }
12

13
    public async run() {
14
        if (await fsExtra.pathExists(this.cwd) === false) {
5!
15
            throw new Error(`"${this.cwd}" does not exist`);
×
16
        }
17
        await this.getRootDirFromUser();
5✔
18
        await util.spawnNpmAsync([
5✔
19
            'init',
20
            this.force ? '--force' : undefined
5✔
21
        ], {
22
            cwd: this.cwd,
23
            stdio: 'inherit'
24
        });
25
    }
26

27
    private get cwd() {
28
        if (this.args?.cwd) {
17!
29
            return path.resolve(process.cwd(), this.args?.cwd);
17!
30
        } else {
31
            return process.cwd();
×
32
        }
33
    }
34

35
    private get force() {
36
        return this.args.force === true;
9✔
37
    }
38

39
    private get promptForRootDir() {
40
        //explicitly check for "true", because undefined is valid and means "default to value of force"
41
        return this.args.promptForRootDir === true || this.force === false;
4✔
42
    }
43

44
    /**
45
     * If the package.json is missing `ropm.rootDir`, prompt user for that info
46
     */
47
    private async getRootDirFromUser() {
48

49
        const packagePath = path.join(this.cwd, 'package.json');
5✔
50
        let packageJson = {} as RopmPackageJson;
5✔
51
        if (fsExtra.pathExistsSync(packagePath)) {
5✔
52
            packageJson = await util.getPackageJson(this.cwd);
2✔
53
        }
54
        if (!packageJson.ropm) {
5✔
55
            packageJson.ropm = {};
4✔
56
        }
57
        //if there is no rootDir present
58
        if (!packageJson.ropm.rootDir) {
5✔
59
            if (this.promptForRootDir) {
4✔
60
                const answer = await util.getUserInput(`What is the rootDir for your project (./):`);
2✔
61
                packageJson.ropm.rootDir = answer.trim() || './';
2!
62
            } else {
63
                //default to current directory
64
                packageJson.ropm.rootDir = './';
2✔
65
            }
66
        }
67
        fsExtra.writeJsonSync(packagePath, packageJson, {
5✔
68
            spaces: 4
69
        });
70
    }
71
}
72

73
export interface InitCommandArgs {
74
    /**
75
     * The current working directory for the command.
76
     */
77
    cwd?: string;
78
    /**
79
     * If true, then generate without any questions
80
     */
81
    force?: boolean;
82
    /**
83
     * This overrides `force` for rootDir, but only if present
84
     */
85
    promptForRootDir?: boolean;
86
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc