Description
TypeScript Version: 1.8.0
Code
I have two projects. One I'm building general gulp tasks in. One I'm consuming those general gulp tasks.
Project 1 contains a class called TasksTools that lives in a file called tasksTools.ts
import { existsSync, lstatSync, readdirSync } from 'fs';
import * as gulp from 'gulp';
import * as util from 'gulp-util';
import * as isstream from 'isstream';
import { join } from 'path';
import * as tildify from 'tildify';
export class TasksTools {
private _gulpConfig: any;
constructor(config: any) {
this._gulpConfig = config;
}
/**
* Loads the tasks within the given path.
* @param {string} path - The path to load the tasks from.
* @param {any} config - The configuration file.
*/
public loadTasksFromDir(path: string, config: any): void {
this._readDir(path, taskname => this.registerTask(taskname, path, config));
}
/**
* Registers the task by the given taskname and path.
* @param {string} taskname - The name of the task.
* @param {string} path - The path of the task.
* @param {any} config - The configuration file.
*/
public registerTask(taskname: string, path: string, config: any): void {
const TASK = join(path, taskname);
gulp.task(taskname, (done: any) => {
const task = require(TASK);
if (task.length > 0) {
return task(config, done);
}
const taskReturnedValue = task();
if (isstream(taskReturnedValue)) {
return taskReturnedValue;
}
// TODO: add promise handling if needed at some point.
done();
});
}
/**
* Reads the files in the given root directory and executes the given callback per found file.
* @param {string} root - The root directory to read.
* @param {function} cb - The callback to execute per found file.
*/
private _readDir(root: string, cb: (taskname: string) => void): void {
if (!existsSync(root)) {
return;
}
walk(root);
function walk(path: string) {
let files = readdirSync(path);
for (let i = 0; i < files.length; i += 1) {
let file = files[i];
let curPath = join(path, file);
if (lstatSync(curPath).isFile() && /\.ts$/.test(file)) {
let taskname = file.replace(/\.ts$/, '');
cb(taskname);
}
else if (lstatSync(curPath).isDirectory()) { // recurse
walk(curPath);
}
}
}
}
}
it also has a definitions file that lives in a file called index.d.ts
declare module "@mtm/mtm-ng2-gulp-tasks" {
export class TasksTools {
public constructor(config: any)
/**
* Loads the tasks within the given path.
* @param {string} path - The path to load the tasks from.
* @param {any} config - The configuration file.
*/
public loadTasksFromDir(path: string, config: any): void
/**
* Registers the task by the given taskname and path.
* @param {string} taskname - The name of the task.
* @param {string} path - The path of the task.
* @param {any} config - The configuration file.
*/
public registerTask(taskname: string, path: string, config: any): void
}
}
so I wrap these up and make an NPM library. Then I import that lib into project 2
Here is my gulp file gulpfile.ts
import * as gulp from 'gulp';
import * as config from './gulp.config';
import * as runSequence from 'run-sequence';
import * as util from 'gulp-util';
// This line doesn't work
import { TasksTools } from '@mtm/mtm-ng2-gulp-tasks';
// If I uncomment this line it works
//import { TasksTools } from './node_modules/@mtm/mtm-ng2-gulp-tasks/utils/momentum.utils';
let _gulpConfig = new config.ProjectConfig();
let _taskTools = new TasksTools(_gulpConfig);
// Load Default Tasks
_taskTools.loadTasksFromDir(_gulpConfig.MOMENTUM_TASKS_DIR, _gulpConfig);
// Load Project Tasks
_taskTools.loadTasksFromDir(_gulpConfig.PROJECT_TASKS_DIR, _gulpConfig);
Now when I try to run this I get the error
Failed to run "C:\TFS\Dev\Momentum.Torques\Client\Gulp\mtm-ng2-gulp-tasks\mtm-ng2-gulp-tasks-test\Gulpfile.js"...
cmd.exe /c gulp --tasks-simple
module.js:442
throw err;
^
Error: Cannot find module '@mtm/mtm-ng2-gulp-tasks'
Expected behavior:
I expect that this would work
Actual behavior:
It did not work