Skip to content
This repository was archived by the owner on May 1, 2020. It is now read-only.

Commit 265f330

Browse files
committed
refactor(enviromental variables): use constants instead of strings
use constants instead of strings
1 parent 5944af7 commit 265f330

File tree

6 files changed

+26
-25
lines changed

6 files changed

+26
-25
lines changed

src/build.spec.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import * as Constants from './util/constants';
12
import { BuildContext } from './util/interfaces';
2-
33
import * as helpers from './util/helpers';
44
import * as clean from './clean';
55
import * as build from './build';
@@ -87,8 +87,8 @@ describe('build', () => {
8787

8888
describe('test project requirements before building', () => {
8989
it('should fail if APP_ENTRY_POINT file does not exist', () => {
90-
process.env.IONIC_APP_ENTRY_POINT = 'src/app/main.ts';
91-
process.env.IONIC_TS_CONFIG = 'tsConfig.js';
90+
process.env[Constants.ENV_APP_ENTRY_POINT] = 'src/app/main.ts';
91+
process.env[Constants.ENV_TS_CONFIG] = 'tsConfig.js';
9292
const error = new Error('App entry point was not found');
9393

9494
spyOn(helpers, 'readFileAsync').and.returnValue(Promise.reject(error));
@@ -100,8 +100,8 @@ describe('test project requirements before building', () => {
100100
});
101101

102102
it('should fail if IONIC_TS_CONFIG file does not exist', () => {
103-
process.env.IONIC_APP_ENTRY_POINT = 'src/app/main.ts';
104-
process.env.IONIC_TS_CONFIG = 'tsConfig.js';
103+
process.env[Constants.ENV_APP_ENTRY_POINT] = 'src/app/main.ts';
104+
process.env[Constants.ENV_TS_CONFIG] = 'tsConfig.js';
105105
const error = new Error('App entry point was not found');
106106

107107
spyOn(helpers, 'readFileAsync').and.callFake((filePath: string) => {
@@ -118,8 +118,8 @@ describe('test project requirements before building', () => {
118118
});
119119

120120
it('should fail fataly if IONIC_TS_CONFIG file does not contain valid JSON', () => {
121-
process.env.IONIC_APP_ENTRY_POINT = 'src/app/main.ts';
122-
process.env.IONIC_TS_CONFIG = 'tsConfig.js';
121+
process.env[Constants.ENV_APP_ENTRY_POINT] = 'src/app/main.ts';
122+
process.env[Constants.ENV_TS_CONFIG] = 'tsConfig.js';
123123
spyOn(helpers, 'readFileAsync').and.callFake(() => {
124124
return Promise.resolve(`{
125125
"compilerOptions" {
@@ -136,8 +136,8 @@ describe('test project requirements before building', () => {
136136
});
137137

138138
it('should fail fataly if IONIC_TS_CONFIG file does not contain compilerOptions.sourceMap === true', () => {
139-
process.env.IONIC_APP_ENTRY_POINT = 'src/app/main.ts';
140-
process.env.IONIC_TS_CONFIG = 'tsConfig.js';
139+
process.env[Constants.ENV_APP_ENTRY_POINT] = 'src/app/main.ts';
140+
process.env[Constants.ENV_TS_CONFIG] = 'tsConfig.js';
141141
spyOn(helpers, 'readFileAsync').and.callFake(() => {
142142
return Promise.resolve(`{
143143
"compilerOptions": {
@@ -154,8 +154,8 @@ describe('test project requirements before building', () => {
154154
});
155155

156156
it('should succeed if IONIC_TS_CONFIG file contains compilerOptions.sourceMap === true', () => {
157-
process.env.IONIC_APP_ENTRY_POINT = 'src/app/main.ts';
158-
process.env.IONIC_TS_CONFIG = 'tsConfig.js';
157+
process.env[Constants.ENV_APP_ENTRY_POINT] = 'src/app/main.ts';
158+
process.env[Constants.ENV_TS_CONFIG] = 'tsConfig.js';
159159

160160
spyOn(clean, 'clean');
161161
spyOn(copy, 'copy').and.returnValue(Promise.resolve());

src/build.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { FILE_CHANGE_EVENT, FILE_DELETE_EVENT } from './util/constants';
1+
import * as Constants from './util/constants';
22
import { BuildContext, BuildState, BuildUpdateMessage, ChangedFile } from './util/interfaces';
33
import { BuildError } from './util/errors';
4+
import { emit, EventType } from './util/events';
45
import { readFileAsync, setContext } from './util/helpers';
56
import { bundle, bundleUpdate } from './bundle';
67
import { clean } from './clean';
78
import { copy } from './copy';
8-
import { emit, EventType } from './util/events';
99
import { lint, lintUpdate } from './lint';
1010
import { Logger } from './logger/logger';
1111
import { minifyCss, minifyJs } from './minify';
@@ -44,17 +44,17 @@ function buildWorker(context: BuildContext) {
4444

4545
function validateRequiredFilesExist() {
4646
return Promise.all([
47-
readFileAsync(process.env.IONIC_APP_ENTRY_POINT),
48-
readFileAsync(process.env.IONIC_TS_CONFIG)
47+
readFileAsync(process.env[Constants.ENV_APP_ENTRY_POINT]),
48+
readFileAsync(process.env[Constants.ENV_TS_CONFIG])
4949
]).catch((error) => {
50-
if (error.code === 'ENOENT' && error.path === process.env.IONIC_APP_ENTRY_POINT) {
50+
if (error.code === 'ENOENT' && error.path === process.env[Constants.ENV_APP_ENTRY_POINT]) {
5151
error = new BuildError(`${error.path} was not found. The "main.dev.ts" and "main.prod.ts" files have been deprecated. Please create a new file "main.ts" containing the content of "main.dev.ts", and then delete the deprecated files.
5252
For more information, please see the default Ionic project main.ts file here:
5353
https://github.com/driftyco/ionic2-app-base/tree/master/src/app/main.ts`);
5454
error.isFatal = true;
5555
throw error;
5656
}
57-
if (error.code === 'ENOENT' && error.path === process.env.IONIC_TS_CONFIG) {
57+
if (error.code === 'ENOENT' && error.path === process.env[Constants.ENV_TS_CONFIG]) {
5858
error = new BuildError([`${error.path} was not found. The "tsconfig.json" file is missing. This file is required.`,
5959
'For more information please see the default Ionic project tsconfig.json file here:',
6060
'https://github.com/driftyco/ionic2-app-base/blob/master/tsconfig.json'].join('\n'));
@@ -262,7 +262,7 @@ function buildUpdateTasks(changedFiles: ChangedFile[], context: BuildContext) {
262262
// we need to do a sass update
263263
return sassUpdate(changedFiles, context).then(outputCssFile => {
264264
const changedFile: ChangedFile = {
265-
event: FILE_CHANGE_EVENT,
265+
event: Constants.FILE_CHANGE_EVENT,
266266
ext: '.css',
267267
filePath: outputCssFile
268268
};
@@ -276,7 +276,7 @@ function buildUpdateTasks(changedFiles: ChangedFile[], context: BuildContext) {
276276
// we need to do a full sass build
277277
return sass(context).then(outputCssFile => {
278278
const changedFile: ChangedFile = {
279-
event: FILE_CHANGE_EVENT,
279+
event: Constants.FILE_CHANGE_EVENT,
280280
ext: '.css',
281281
filePath: outputCssFile
282282
};
@@ -298,7 +298,7 @@ function loadFiles(changedFiles: ChangedFile[], context: BuildContext) {
298298
// UPDATE IN-MEMORY FILE CACHE
299299
let promises: Promise<any>[] = [];
300300
for (const changedFile of changedFiles) {
301-
if (changedFile.event === FILE_DELETE_EVENT) {
301+
if (changedFile.event === Constants.FILE_DELETE_EVENT) {
302302
// remove from the cache on delete
303303
context.fileCache.remove(changedFile.filePath);
304304
} else {

src/copy.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { mkdirpSync } from 'fs-extra';
22
import { dirname as pathDirname, join as pathJoin, relative as pathRelative, resolve as pathResolve } from 'path';
33
import { Logger } from './logger/logger';
44
import { fillConfigDefaults, generateContext, getUserConfigFile, replacePathVars } from './util/config';
5+
import * as Constants from './util/constants';
56
import { emit, EventType } from './util/events';
67
import { generateGlobTasks, globAll, GlobObject, GlobResult } from './util/glob-util';
78
import { copyFileAsync, rimRafAsync, unlinkAsync } from './util/helpers';
@@ -40,7 +41,7 @@ export function copyWorker(context: BuildContext, configFile: string) {
4041
// basically, we've got a stew goin'
4142
return populateFileAndDirectoryInfo(resultMap, copyConfig, toCopyList, directoriesToCreate);
4243
}).then(() => {
43-
if (process.env.IONIC_CLEAN_BEFORE_COPY) {
44+
if (process.env[Constants.ENV_CLEAN_BEFORE_COPY]) {
4445
cleanDirectories(context, directoriesToCreate);
4546
}
4647
}).then(() => {

src/ngc.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Logger } from './logger/logger';
22
import { getUserConfigFile} from './util/config';
3+
import * as Constants from './util/constants';
34
import { BuildContext, TaskInfo } from './util/interfaces';
45
import { AotCompiler } from './aot/aot-compiler';
56

@@ -18,7 +19,7 @@ export function ngc(context: BuildContext, configFile?: string) {
1819
}
1920

2021
export function ngcWorker(context: BuildContext, configFile: string) {
21-
const compiler = new AotCompiler(context, { entryPoint: process.env.IONIC_APP_ENTRY_POINT, rootDir: context.rootDir, tsConfigPath: process.env.IONIC_TS_CONFIG });
22+
const compiler = new AotCompiler(context, { entryPoint: process.env[Constants.ENV_APP_ENTRY_POINT], rootDir: context.rootDir, tsConfigPath: process.env[Constants.ENV_TS_CONFIG] });
2223
return compiler.compile();
2324
}
2425

src/util/config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,6 @@ export function setAppPackageJsonData(data: any) {
409409
appPackageJsonData = data;
410410
}
411411

412-
413412
function getAppPackageJsonData(context: BuildContext) {
414413
if (!appPackageJsonData) {
415414
try {

src/webpack/source-mapper.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { BuildContext } from '../util/interfaces';
22
import { getContext, toUnixPath} from '../util/helpers';
33
import { join, normalize, relative, resolve, sep } from 'path';
4-
import { SOURCE_MAP_TYPE_CHEAP } from '../util/constants';
4+
import { SOURCE_MAP_TYPE_CHEAP, ENV_VAR_SOURCE_MAP_TYPE } from '../util/constants';
55

66
export function provideCorrectSourcePath(webpackObj: any) {
77
const context = getContext();
@@ -12,7 +12,7 @@ function provideCorrectSourcePathInternal(webpackObj: any, context: BuildContext
1212
const webpackResourcePath = webpackObj.resourcePath;
1313
const noTilde = webpackResourcePath.replace(/~/g, 'node_modules');
1414
const absolutePath = resolve(normalize(noTilde));
15-
if (process.env.IONIC_SOURCE_MAP_TYPE === SOURCE_MAP_TYPE_CHEAP) {
15+
if (process.env[ENV_VAR_SOURCE_MAP_TYPE] === SOURCE_MAP_TYPE_CHEAP) {
1616
const mapPath = sep + absolutePath;
1717
return toUnixPath(mapPath);
1818
}

0 commit comments

Comments
 (0)