Skip to content

Commit

Permalink
Removing need for symlinkg and using Ionic's build tools
Browse files Browse the repository at this point in the history
  • Loading branch information
lathonez committed Mar 3, 2016
1 parent 6003691 commit 9005006
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 23 deletions.
1 change: 0 additions & 1 deletion build

This file was deleted.

116 changes: 97 additions & 19 deletions gulpfile.ts
Original file line number Diff line number Diff line change
@@ -1,71 +1,149 @@
import { APP_DIR, TEST_DIR, TYPINGS_DIR, TEST_DEST } from './test/config';
import { APP_DIR, DIST_DIR, JS_DEST, TEST_DIR, TYPINGS_DIR, TEST_DEST } from './test/config';
import { join } from 'path';
import * as runSequence from 'run-sequence';
import { accessSync } from 'fs';
import * as chalk from 'chalk';
import * as del from 'del';
import * as gulp from 'gulp';
import * as ionicAppLib from 'ionic-app-lib';
import * as karma from 'karma';
import * as loadPlugins from 'gulp-load-plugins';
import * as ts from 'gulp-typescript';
import * as tslint from 'gulp-tslint';
import * as runSequence from 'run-sequence';
import * as util from 'gulp-util';

let plugins: any = loadPlugins();
let ionicBuild: any = ionicAppLib.v2.build;
let ionicBuildOptions: any = {
appDirectory: process.cwd(),
callback: (): void => {
util.log(chalk.green('√ Compiling files complete.'));
},
watch: false,
config: require(join(process.cwd(), 'ionic.config.js')),
};

// compile typescript into indivudal files, project directoy structure is replicated under www/build/test
function build() {
let tsProject = plugins.typescript.createProject('tsconfig.json', {
// compile typescript into individual files, project directoy structure is replicated under www/build/test
function build(): any {
'use strict';

let tsProject: any = plugins.typescript.createProject('tsconfig.json', {
typescript: require('typescript')
});
let src = [
let src: Array<any> = [
join(APP_DIR, '**/*.ts'),
join(TEST_DIR, '**/*.ts'),
join(TYPINGS_DIR, '/browser.d.ts'),
];
let result = gulp.src(src)
.pipe(plugins.inlineNg2Template({ useRelativePaths: false }))
let result: any = gulp.src(src)
.pipe(plugins.inlineNg2Template({ base: 'www', useRelativePaths: false }))
.pipe(plugins.typescript(tsProject));

return result.js
.pipe(gulp.dest(TEST_DEST));
}

// this does the exact same thing as `ionic serve` (wrt building fonts)
function buildFonts(): any {
'use strict';

return ionicBuild.fonts(ionicBuildOptions);
}

// this does the exact same thing as `ionic serve` (wrt building html)
function buildHTML(): any {
'use strict';

return ionicBuild.html(ionicBuildOptions);
}

// ionic's build functions are async but don't give us any callbacks
function buildLocker(done: Function): any {
'use strict';

let unlocked: boolean = true;
let files: Array<{}> = [
{'path': join(DIST_DIR, 'fonts', 'ionicons.ttf'), 'waits': 'fonts'},
{'path': join(DIST_DIR, 'app.html'), 'waits': 'html'},
{'path': join(DIST_DIR, 'css', 'app.md.css'), 'waits': 'css'},
]; // files we expect to exist after building

let recurse: Function = function(): void {
buildLocker(done);
};

files.some((file: {}, index: number, array: Array<string>) => {
try {
accessSync(file['path']);
} catch (e) {
util.log('buildLocker:' + chalk.yellow(' waiting for ' + file['waits'] + ' build to complete'));
setTimeout(recurse, 100);
unlocked = false;
return true;
}
});

if (!unlocked) {
return;
}

util.log('buildLocker:' + chalk.green(' unlocked'));
done();
}

// this does the exact same thing as `ionic serve` (wrt building sass)
function buildSass(): any {
'use strict';

return ionicBuild.sass(ionicBuildOptions);
}

// typescript files are compiled individually and saved to www/build/test/ - delete them here
function clean(done) {
function clean(done: Function): any {
'use strict';

// You can use multiple globbing patterns as you would with `gulp.src`
return del([TEST_DEST]).then((paths) => {
return del([DIST_DIR + '**/*', '!' + JS_DEST]).then((paths: Array<any>) => {
util.log('Deleted', chalk.yellow(paths && paths.join(', ') || '-'));
});
}

// run tslint against all typescript
function lint() {
function lint(): any {
'use strict';

return gulp.src(join(APP_DIR, '**/*.ts'))
.pipe(plugins.tslint())
.pipe(plugins.tslint.report(plugins.tslintStylish, {
emitError: true,
sort: true,
bell: true
bell: true,
}));
}

// run jasmine unit tests using karma - lint and build are run in parallel
function startKarma(done) {
function startKarma(done: Function): any {
'use strict';

new (<any>karma).Server({
configFile: join(process.cwd(), TEST_DIR, 'karma.config.js')
}).start(done);
}

gulp.task('startKarma', startKarma);
gulp.task('test.build', build);
gulp.task('test.build.fonts', buildFonts);
gulp.task('test.build.html', buildHTML);
gulp.task('test.build.locker', buildLocker);
gulp.task('test.build.sass', buildSass);
gulp.task('test.clean', clean);
gulp.task('test.lint', lint);
gulp.task('test.build', build);
gulp.task('startKarma', startKarma);

gulp.task('test', (done) => {
gulp.task('test', (done: any) => {
runSequence(
['test.clean', 'test.lint'],
['test.build.html', 'test.build.fonts', 'test.build.sass'],
'test.build.locker',
'test.build',
'startKarma',
done
)
);
});
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"gulp-inline-ng2-template": "^1.1.2",
"gulp-load-plugins": "1.2.0",
"gulp-tslint": "4.3.2",
"gulp-typescript": "2.12.0",
"gulp-typescript": "^2.12.1",
"ionic-app-lib": "^2.0.0-beta.9",
"jasmine-core": "2.4.1",
"karma": "0.13.21",
"karma-chrome-launcher": "0.2.2",
Expand Down
3 changes: 2 additions & 1 deletion test/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export const APP_DIR = 'app';
export const TEST_DIR = 'test'
export const TYPINGS_DIR = 'typings'
export const DIST_DIR = 'www/build';
export const TEST_DEST = `${DIST_DIR}/test`;
export const JS_DEST = `${DIST_DIR}/test`;
export const TEST_DEST = `${DIST_DIR}/test`;
7 changes: 7 additions & 0 deletions test/ionic-app-lib.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
declare module "ionic-app-lib" {
module IonicAppLib {
var v2: any;
}

export = IonicAppLib;
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"filesGlob": [
"./app/**/*.ts",
"!node_modules/**/*",
"typings/browser.d.ts"
"typings/browser.d.ts",
"test/ionic-app-lib.d.ts"
],
"exclude": [
"node_modules"
Expand Down

0 comments on commit 9005006

Please sign in to comment.