UNPKG

4.13 kBJavaScriptView Raw
1const { DEFAULT_EXT, DEFAULT_TABLE_NAME } = require('./constants');
2const { resolveClientNameWithAliases } = require('../../lib/helpers');
3const fs = require('fs');
4const path = require('path');
5const tildify = require('tildify');
6const color = require('colorette');
7const argv = require('getopts')(process.argv.slice(2));
8
9function mkConfigObj(opts) {
10 if (!opts.client) {
11 const path = resolveDefaultKnexfilePath();
12 throw new Error(
13 `No default configuration file '${path}' found and no commandline connection parameters passed`
14 );
15 }
16
17 const envName = opts.env || process.env.NODE_ENV || 'development';
18 const resolvedClientName = resolveClientNameWithAliases(opts.client);
19 const useNullAsDefault = resolvedClientName === 'sqlite3';
20 return {
21 ext: DEFAULT_EXT,
22 [envName]: {
23 useNullAsDefault,
24 client: opts.client,
25 connection: opts.connection,
26 migrations: {
27 directory: opts.migrationsDirectory,
28 tableName: opts.migrationsTableName || DEFAULT_TABLE_NAME,
29 },
30 },
31 };
32}
33
34function resolveKnexFilePath() {
35 const jsPath = resolveDefaultKnexfilePath('js');
36 if (fs.existsSync(jsPath)) {
37 return {
38 path: jsPath,
39 extension: 'js',
40 };
41 }
42
43 const tsPath = resolveDefaultKnexfilePath('ts');
44 if (fs.existsSync(tsPath)) {
45 return {
46 path: tsPath,
47 extension: 'ts',
48 };
49 }
50
51 console.warn(
52 `Failed to find configuration at default location of ${resolveDefaultKnexfilePath(
53 'js'
54 )}`
55 );
56}
57
58function resolveDefaultKnexfilePath(extension) {
59 return process.cwd() + `/knexfile.${extension}`;
60}
61
62function resolveEnvironmentConfig(opts, allConfigs) {
63 const environment = opts.env || process.env.NODE_ENV || 'development';
64 const result = allConfigs[environment] || allConfigs;
65
66 if (allConfigs[environment]) {
67 console.log('Using environment:', color.magenta(environment));
68 }
69
70 if (!result) {
71 console.log(color.red('Warning: unable to read knexfile config'));
72 process.exit(1);
73 }
74
75 if (argv.debug !== undefined) {
76 result.debug = argv.debug;
77 }
78
79 return result;
80}
81
82function exit(text) {
83 if (text instanceof Error) {
84 console.error(
85 color.red(`${text.detail ? `${text.detail}\n` : ''}${text.stack}`)
86 );
87 } else {
88 console.error(color.red(text));
89 }
90 process.exit(1);
91}
92
93function success(text) {
94 console.log(text);
95 process.exit(0);
96}
97
98function checkLocalModule(env) {
99 if (!env.modulePath) {
100 console.log(
101 color.red('No local knex install found in:'),
102 color.magenta(tildify(env.cwd))
103 );
104 exit('Try running: npm install knex');
105 }
106}
107
108function getMigrationExtension(env, opts) {
109 const config = resolveEnvironmentConfig(opts, env.configuration);
110
111 let ext = DEFAULT_EXT;
112 if (argv.x) {
113 ext = argv.x;
114 } else if (config.migrations && config.migrations.extension) {
115 ext = config.migrations.extension;
116 } else if (config.ext) {
117 ext = config.ext;
118 }
119 return ext.toLowerCase();
120}
121
122function getSeedExtension(env, opts) {
123 const config = resolveEnvironmentConfig(opts, env.configuration);
124
125 let ext = DEFAULT_EXT;
126 if (argv.x) {
127 ext = argv.x;
128 } else if (config.seeds && config.seeds.extension) {
129 ext = config.seeds.extension;
130 } else if (config.ext) {
131 ext = config.ext;
132 }
133 return ext.toLowerCase();
134}
135
136function getStubPath(configKey, env, opts) {
137 const config = resolveEnvironmentConfig(opts, env.configuration);
138 const stubDirectory = config[configKey] && config[configKey].directory;
139
140 const { stub } = argv;
141 if (!stub) {
142 return null;
143 } else if (stub.includes('/')) {
144 // relative path to stub
145 return stub;
146 }
147
148 // using stub <name> must have config[configKey].directory defined
149 if (!stubDirectory) {
150 console.log(color.red('Failed to load stub'), color.magenta(stub));
151 exit(`config.${configKey}.directory in knexfile must be defined`);
152 }
153
154 return path.join(stubDirectory, stub);
155}
156
157module.exports = {
158 mkConfigObj,
159 resolveKnexFilePath,
160 resolveEnvironmentConfig,
161 exit,
162 success,
163 checkLocalModule,
164 getSeedExtension,
165 getMigrationExtension,
166 getStubPath,
167};