1 | #!/usr/bin/env node
|
2 |
|
3 | var path = require('path'),
|
4 | fs = require('fs');
|
5 |
|
6 | var unixify = require('unixify');
|
7 | var cjson = require('cjson');
|
8 |
|
9 | var logger = require('note-down');
|
10 | logger.removeOption('showLogLine');
|
11 | var chalk = logger.chalk;
|
12 |
|
13 | var argv = require('yargs')
|
14 | .help(false)
|
15 | .version(false)
|
16 | .argv;
|
17 |
|
18 | var utils = require('./utils.js');
|
19 | var main = require('./main.js');
|
20 |
|
21 | var paramHelp = argv.h || argv.help,
|
22 | paramVersion = argv.v || argv.version,
|
23 | paramVerbose = argv.verbose,
|
24 | paramOutdated = argv.outdated,
|
25 | paramWhenFileExists = argv.whenFileExists;
|
26 |
|
27 | var packageJson = require('./package.json');
|
28 |
|
29 | var nodeVersion = process.versions.node;
|
30 |
|
31 | var cwd = unixify(process.cwd());
|
32 |
|
33 | if (!module.parent) {
|
34 | var showHelp = function () {
|
35 | logger.log([
|
36 | '',
|
37 | chalk.bold('Usage:'),
|
38 | ' copy-files-from-to [--config <config-file>] [--mode <mode-name>] [...]',
|
39 | '',
|
40 | chalk.bold('Examples:'),
|
41 | ' copy-files-from-to',
|
42 | ' copy-files-from-to --config copy-files-from-to.json',
|
43 | ' copy-files-from-to --mode production',
|
44 | ' copy-files-from-to -h',
|
45 | ' copy-files-from-to --version',
|
46 | '',
|
47 | chalk.bold('Options:'),
|
48 | ' --config <config-file-path> Path to configuration file',
|
49 | ' When unspecified, it looks for:',
|
50 | ' 1) copy-files-from-to.cjson',
|
51 | ' 2) copy-files-from-to.json',
|
52 | ' 3) package.json',
|
53 | ' --mode <mode-name> Mode to use for copying the files',
|
54 | ' When unspecified, it uses "default" mode',
|
55 | ' --when-file-exists <operation> Override "whenFileExists" setting specified in configuration file',
|
56 | ' <operation> can be "notify-about-available-change" or "overwrite" or "do-nothing"',
|
57 | ' --outdated Notify about outdated parts of the configuration file',
|
58 | ' (takes cue from "latest" property, wherever specified)',
|
59 | ' --verbose Verbose logging',
|
60 | ' -v --version Output the version number',
|
61 | ' -h --help Show help',
|
62 | ''
|
63 | ].join('\n'));
|
64 | };
|
65 |
|
66 | if (paramHelp) {
|
67 | showHelp();
|
68 | process.exit(0);
|
69 | }
|
70 |
|
71 | if (paramVersion || paramVerbose) {
|
72 | logger.log(packageJson.name + ' version: ' + packageJson.version);
|
73 | logger.log('Node JS version: ' + nodeVersion);
|
74 | if (paramVersion) {
|
75 | process.exit(0);
|
76 | }
|
77 | }
|
78 |
|
79 | var configFile = null;
|
80 |
|
81 | configFile = argv.config;
|
82 | if (!configFile) {
|
83 | if (fs.existsSync(path.resolve(cwd, 'copy-files-from-to.cjson'))) {
|
84 | configFile = 'copy-files-from-to.cjson';
|
85 | } else if (fs.existsSync(path.resolve(cwd, 'copy-files-from-to.json'))) {
|
86 | configFile = 'copy-files-from-to.json';
|
87 | } else if (fs.existsSync(path.resolve(cwd, 'package.json'))) {
|
88 | configFile = 'package.json';
|
89 | } else {
|
90 | logger.error(
|
91 | '\n' +
|
92 | chalk.bold('Error:') + ' Please ensure that you have passed correct arguments. Exiting with error (code 1).'
|
93 | );
|
94 | showHelp();
|
95 | process.exit(1);
|
96 | }
|
97 | }
|
98 |
|
99 | var configFileSource,
|
100 | configFileSourceDirectory;
|
101 |
|
102 | if (configFile.indexOf('/') === 0 || configFile.indexOf('\\') === 0) {
|
103 | configFileSource = configFile;
|
104 | } else {
|
105 | configFileSource = path.resolve(cwd, configFile);
|
106 | }
|
107 | configFileSourceDirectory = unixify(path.dirname(configFileSource));
|
108 |
|
109 | var cjsonText;
|
110 | try {
|
111 | logger.info('Reading copy instructions from file ' + utils.getRelativePath(cwd, configFileSource));
|
112 | cjsonText = fs.readFileSync(configFileSource, 'utf8');
|
113 | cjsonText = utils.stripBom(cjsonText);
|
114 | } catch (e) {
|
115 | utils.exitWithError(e, 'Error in reading file: ' + configFileSource);
|
116 | }
|
117 |
|
118 | var copyFiles = [],
|
119 | copyFilesSettings = {};
|
120 | try {
|
121 | var cjsonData = cjson.parse(cjsonText);
|
122 | if (cjsonData instanceof Object) {
|
123 | if (Array.isArray(cjsonData.copyFiles)) {
|
124 | copyFiles = cjsonData.copyFiles;
|
125 | }
|
126 | if (cjsonData.copyFilesSettings instanceof Object) {
|
127 | copyFilesSettings = cjsonData.copyFilesSettings;
|
128 | }
|
129 | }
|
130 | } catch (e) {
|
131 | utils.exitWithError(e, 'Invalid (C)JSON data:\n ' + cjsonText.replace(/\n/g, '\n '));
|
132 | }
|
133 |
|
134 | main({
|
135 | paramVerbose: paramVerbose,
|
136 | paramOutdated: paramOutdated,
|
137 | paramWhenFileExists: paramWhenFileExists,
|
138 | cwd: cwd,
|
139 | copyFiles: copyFiles,
|
140 | copyFilesSettings: copyFilesSettings,
|
141 | configFileSourceDirectory: configFileSourceDirectory,
|
142 | mode: argv.mode
|
143 | });
|
144 | }
|
145 |
|
146 | module.exports = main;
|