-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·85 lines (73 loc) · 3.07 KB
/
index.js
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
84
85
#!/usr/bin/env node
'use strict';
const program = require('commander');
const chalk = require('chalk');
const getDistance = require('levenshtein-lite');
const commands = require('./src/commands/index.bs');
const OutputPatcher = require('./src/patches').OutputPatcher;
const rootDirectory = process.cwd();
const handleSetup = commands.handleSetup();
const handleLink = commands.handleLink();
const handleConfig = commands.handleConfig();
const handleLinter = commands.handleLinter();
const VERSION = require('./src/config').VERSION;
const unknownCommand = program => !program.args.map(arg => typeof arg).includes('object')
program.version(VERSION)
.usage('add-reason [command] [options]')
.option('--no-linking', 'don\'t create the symlink to your compiled ReasonML code')
.option('--in-source', 'change target to in-source rather than a compiled directory');
program.command('setup <directory> [package-name]')
.description('set up Reason directory, config files, and symlink')
.action((directory, name) => {
// given name or last source directory path name or simply `pkg`
name = name || directory.replace(/\/+$/, '').split('/').pop() || 'pkg';
let inSource = program.inSource;
let linking = inSource ? false : program.linking;
handleSetup(name, directory, rootDirectory, VERSION, inSource, linking);
});
program.command('link <directory> [package-name]')
.description('create a symlink with the given package name')
.action((directory, name) => {
// given name or last source directory path name or simply `pkg`
name = name || directory.replace(/\/+$/, '').split('/').pop() || 'pkg';
handleLink(name, directory, rootDirectory, VERSION);
});
program.command('linter <directory>')
.description('create a merlin file for linting your ReasonML code')
.action(directory => {
handleLinter(directory, rootDirectory, VERSION);
});
program.command('config <directory>')
.description('create a bsconfig file for building your ReasonML code')
.action(directory => {
handleConfig(directory, rootDirectory, !!program.inSource, VERSION);
});
OutputPatcher(program);
program.parse(process.argv);
// Set default command to --help
if (!process.argv.slice(2).length) {
program.outputHelp();
process.exit(0);
}
if (unknownCommand(program)) {
const unknownCommand = program.args[0];
const possibleCommands = program.commands.reduce((acc, cmd) => {
const name = cmd._name;
if (getDistance(name, unknownCommand) < 3) {
acc.push(name);
}
return acc;
}, []);
if (possibleCommands.length > 0) {
process.stdout.write(`\n Unknown command ${chalk.bold(unknownCommand)}. Did you mean to do any of these?\n\n`);
possibleCommands.map(name => process.stdout.write(` ${name}\n`))
process.stdout.write('\n');
} else {
const directory = program.args[0];
const name = program.args[1] || directory.replace(/\/+$/, '').split('/').pop() || 'pkg';
let inSource = program.inSource;
let linking = inSource ? false : program.linking;
handleSetup(name, directory, rootDirectory, VERSION, inSource, linking);
}
process.exit(0);
}