Skip to content

Commit

Permalink
refactor: use process.cwd() instead of process.env.PWD (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
zacharygolba authored Jun 22, 2016
1 parent 5bab51a commit b25237a
Show file tree
Hide file tree
Showing 18 changed files with 90 additions and 106 deletions.
2 changes: 1 addition & 1 deletion bin/lux
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node
'use strict';

const PWD = process.env.PWD;
const PWD = process.cwd();

try {
require(`${PWD}/node_modules/lux-framework/dist/cli`).call(null);
Expand Down
6 changes: 6 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @flow
const { env: ENV } = process;

export const CWD = process.cwd();
export const PORT = parseInt(ENV.PORT, 10) || 4000;
export const NODE_ENV = ENV.NODE_ENV || 'development';
4 changes: 3 additions & 1 deletion src/packages/cli/commands/create.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Ora from 'ora';
import { green } from 'chalk';

import { CWD } from '../../../constants';

import fs from '../../fs';
import template from '../../template';

Expand All @@ -26,7 +28,7 @@ import gitignoreTemplate from '../templates/gitignore';
*/
export default async function create(name, database) {
const driver = driverFor(database);
const project = `${process.env.PWD}/${name}`;
const project = `${CWD}/${name}`;

await fs.mkdirAsync(project);

Expand Down
9 changes: 4 additions & 5 deletions src/packages/cli/commands/db-create.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { CWD, NODE_ENV } from '../../../constants';
import fs from '../../fs';
import loader from '../../loader';
import { connect } from '../../database';

const { env: { PWD, NODE_ENV = 'development' } } = process;

/**
* @private
*/
Expand All @@ -16,12 +15,12 @@ export default async function dbCreate() {
...config
}
}
} = loader(PWD, 'config');
} = loader(CWD, 'config');

if (driver === 'sqlite3') {
await fs.writeFileAsync(`${PWD}/db/${database}_${NODE_ENV}.sqlite`, '');
await fs.writeFileAsync(`${CWD}/db/${database}_${NODE_ENV}.sqlite`, '');
} else {
const { schema } = connect(PWD, { ...config, driver });
const { schema } = connect(CWD, { ...config, driver });
const query = schema.raw(`CREATE DATABASE ${database}`);

query.on('query', () => console.log(query.toString()));
Expand Down
9 changes: 4 additions & 5 deletions src/packages/cli/commands/db-drop.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { CWD, NODE_ENV } from '../../../constants';
import { connect } from '../../database';
import { rmrf } from '../../fs';
import loader from '../../loader';

const { env: { PWD, NODE_ENV = 'development' } } = process;

/**
* @private
*/
Expand All @@ -16,12 +15,12 @@ export default async function dbDrop() {
...config
}
}
} = loader(PWD, 'config');
} = loader(CWD, 'config');

if (driver === 'sqlite3') {
await rmrf(`${PWD}/db/${database}_${NODE_ENV}.sqlite`);
await rmrf(`${CWD}/db/${database}_${NODE_ENV}.sqlite`);
} else {
const { schema } = connect(PWD, { ...config, driver });
const { schema } = connect(CWD, { ...config, driver });
const query = schema.raw(`DROP DATABASE IF EXISTS ${database}`);

query.on('query', () => console.log(query.toString()));
Expand Down
15 changes: 7 additions & 8 deletions src/packages/cli/commands/db-migrate.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
import { CWD } from '../../../constants';
import Database, { pendingMigrations } from '../../database';
import Logger, { sql } from '../../logger';
import loader from '../../loader';

const { env: { PWD } } = process;

/**
* @private
*/
export default async function dbMigrate() {
const { database: config } = loader(PWD, 'config');
const models = loader(PWD, 'models');
const migrations = loader(PWD, 'migrations');
const { database: config } = loader(CWD, 'config');
const models = loader(CWD, 'models');
const migrations = loader(CWD, 'migrations');

const { connection, schema } = await new Database({
config,
models,
path: PWD,
path: CWD,
checkMigrations: false,

logger: await new Logger({
path: PWD,
path: CWD,
enabled: false
})
});

const pending = await pendingMigrations(PWD, () => connection('migrations'));
const pending = await pendingMigrations(CWD, () => connection('migrations'));

if (pending.length) {
await Promise.all(
Expand Down
15 changes: 7 additions & 8 deletions src/packages/cli/commands/db-rollback.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
import { CWD } from '../../../constants';
import Database from '../../database';
import Logger, { sql } from '../../logger';
import fs from '../../fs';
import loader from '../../loader';

const { env: { PWD } } = process;

/**
* @private
*/
export default async function dbRollback() {
const { database: config } = loader(PWD, 'config');
const models = loader(PWD, 'models');
const migrations = loader(PWD, 'migrations');
const { database: config } = loader(CWD, 'config');
const models = loader(CWD, 'models');
const migrations = loader(CWD, 'migrations');

const { connection, schema } = await new Database({
config,
models,
path: PWD,
path: CWD,
checkMigrations: false,

logger: await new Logger({
path: PWD,
path: CWD,
enabled: false
})
});

const migrationFiles = await fs.readdirAsync(`${PWD}/db/migrate`);
const migrationFiles = await fs.readdirAsync(`${CWD}/db/migrate`);

if (migrationFiles.length) {
let migration;
Expand Down
13 changes: 6 additions & 7 deletions src/packages/cli/commands/db-seed.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
import { CWD } from '../../../constants';
import Logger from '../../logger';
import Database from '../../database';
import loader from '../../loader';

const { env: { PWD } } = process;

/**
* @private
*/
export default async function dbSeed() {
const { database: config } = loader(PWD, 'config');
const seed = loader(PWD, 'seed');
const models = loader(PWD, 'models');
const { database: config } = loader(CWD, 'config');
const seed = loader(CWD, 'seed');
const models = loader(CWD, 'models');

await new Database({
config,
models,
path: PWD,
path: CWD,

logger: await new Logger({
path: PWD,
path: CWD,
enabled: false
})
});
Expand Down
13 changes: 6 additions & 7 deletions src/packages/cli/commands/destroy.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { CWD } from '../../../constants';
import { red, green } from 'chalk';
import { pluralize } from 'inflection';

import fs, { rmrf, exists } from '../../fs';

const { env: { PWD } } = process;

/**
* @private
*/
Expand All @@ -19,7 +18,7 @@ export async function destroyType(type, name) {
break;

case 'migration':
const migrations = await fs.readdirAsync(`${PWD}/db/migrate`);
const migrations = await fs.readdirAsync(`${CWD}/db/migrate`);

name = migrations.find(file => `${name}.js` === file.substr(17));
path = `db/migrate/${name}`;
Expand All @@ -32,8 +31,8 @@ export async function destroyType(type, name) {
break;
}

if (await exists(`${PWD}/${path}`)) {
await rmrf(`${PWD}/${path}`);
if (await exists(`${CWD}/${path}`)) {
await rmrf(`${CWD}/${path}`);
console.log(`${red('remove')} ${path}`);
}
}
Expand All @@ -43,7 +42,7 @@ export async function destroyType(type, name) {
*/
export default async function destroy(type, name) {
if (type === 'resource') {
const routes = (await fs.readFileAsync(`${PWD}/app/routes.js`, 'utf8'))
const routes = (await fs.readFileAsync(`${CWD}/app/routes.js`, 'utf8'))
.split('\n')
.reduce((lines, line) => {
const pattern = new RegExp(
Expand All @@ -61,7 +60,7 @@ export default async function destroy(type, name) {
destroyType('controller', name)
]);

await fs.writeFileAsync(`${PWD}/app/routes.js`, routes, 'utf8');
await fs.writeFileAsync(`${CWD}/app/routes.js`, routes, 'utf8');
console.log(`${green('update')} app/routes.js`);
} else if (type === 'model') {
await Promise.all([
Expand Down
39 changes: 19 additions & 20 deletions src/packages/cli/commands/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { red, green, yellow } from 'chalk';
import { pluralize } from 'inflection';
import { createInterface } from 'readline';

import { CWD } from '../../../constants';
import fs, { rmrf, exists } from '../../fs';

import modelTemplate from '../templates/model';
Expand All @@ -13,12 +14,10 @@ import modelMigrationTemplate from '../templates/model-migration';

import indent from '../utils/indent';

const { env: { PWD } } = process;

/**
* @private
*/
export async function generateType(type, name, pwd, attrs = []) {
export async function generateType(type, name, cwd, attrs = []) {
const rl = createInterface({
input: process.stdin,
output: process.stdout
Expand Down Expand Up @@ -74,9 +73,9 @@ export async function generateType(type, name, pwd, attrs = []) {
if (isMigration) {
const regexp = new RegExp(`^\\d+-${path.split('/').pop().substr(17)}$`);

doesExist = await exists(regexp, `${pwd}/db/migrate`);
doesExist = await exists(regexp, `${cwd}/db/migrate`);
} else {
doesExist = await exists(`${pwd}/${path}`);
doesExist = await exists(`${cwd}/${path}`);
}

if (doesExist) {
Expand All @@ -89,7 +88,7 @@ export async function generateType(type, name, pwd, attrs = []) {

if (overwrite) {
if (isMigration) {
const migrations = await fs.readdirAsync(`${pwd}/db/migrate`);
const migrations = await fs.readdirAsync(`${cwd}/db/migrate`);
const isModelMigration = type === 'model-migration';

const oldName = migrations.find(file => {
Expand All @@ -100,21 +99,21 @@ export async function generateType(type, name, pwd, attrs = []) {
if (oldName) {
const oldPath = `db/migrate/${oldName}`;

await rmrf(`${pwd}/${oldPath}`);
await rmrf(`${cwd}/${oldPath}`);
console.log(`${red('remove')} ${oldPath}`);
}

await fs.writeFileAsync(`${pwd}/${path}`, data, 'utf8');
await fs.writeFileAsync(`${cwd}/${path}`, data, 'utf8');
console.log(`${green('create')} ${path}`);
} else {
await fs.writeFileAsync(`${pwd}/${path}`, data, 'utf8');
await fs.writeFileAsync(`${cwd}/${path}`, data, 'utf8');
console.log(`${yellow('overwrite')} ${path}`);
}
} else {
console.log(`${yellow('skip')} ${path}`);
}
} else {
await fs.writeFileAsync(`${pwd}/${path}`, data, 'utf8');
await fs.writeFileAsync(`${cwd}/${path}`, data, 'utf8');
console.log(`${green('create')} ${path}`);
}

Expand All @@ -124,9 +123,9 @@ export async function generateType(type, name, pwd, attrs = []) {
/**
* @private
*/
export default async function generate(type, name, pwd = PWD, attrs = []) {
export default async function generate(type, name, cwd = CWD, attrs = []) {
if (type === 'resource') {
const routes = (await fs.readFileAsync(`${pwd}/app/routes.js`, 'utf8'))
const routes = (await fs.readFileAsync(`${cwd}/app/routes.js`, 'utf8'))
.split('\n')
.reduce((str, line, index, array) => {
const closeIndex = array.lastIndexOf('}');
Expand All @@ -142,17 +141,17 @@ export default async function generate(type, name, pwd = PWD, attrs = []) {
return str;
}, '');

await generateType('model', name, pwd, attrs);
await generateType('model-migration', name, pwd, attrs);
await generateType('serializer', name, pwd, attrs);
await generateType('controller', name, pwd, attrs);
await generateType('model', name, cwd, attrs);
await generateType('model-migration', name, cwd, attrs);
await generateType('serializer', name, cwd, attrs);
await generateType('controller', name, cwd, attrs);

await fs.writeFileAsync(`${pwd}/app/routes.js`, routes, 'utf8');
await fs.writeFileAsync(`${cwd}/app/routes.js`, routes, 'utf8');
console.log(`${green('update')} app/routes.js`);
} else if (type === 'model') {
await generateType(type, name, pwd, attrs);
await generateType('model-migration', name, pwd, attrs);
await generateType(type, name, cwd, attrs);
await generateType('model-migration', name, cwd, attrs);
} else {
await generateType(type, name, pwd, attrs);
await generateType(type, name, cwd, attrs);
}
}
13 changes: 3 additions & 10 deletions src/packages/cli/commands/serve.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
// @flow
import { cyan } from 'chalk';

import { CWD, PORT } from '../../../constants';
import Logger from '../../logger';
import { createCluster } from '../../pm';

const { env: { PWD, PORT } } = process;

/**
* @private
*/
export default async function serve(
port: ?number | ?string = PORT
): Promise<void> {
let path = PWD;
export default async function serve(port: number = PORT): Promise<void> {
let path = CWD;

if (typeof path !== 'string') {
path = __dirname;
}

if (typeof port !== 'number') {
port = 4000;
}

const logger = await new Logger({
path,
enabled: true
Expand Down
Loading

0 comments on commit b25237a

Please sign in to comment.