mc.registerTask(function lint(params: any, callback: (err: Error, res: any) => void) { mc.assertType(params, 'object', 'params'); mc.assertType(params.filePath, 'string', 'params.filePath'); mc.assertType(params.options, 'object', 'params.options'); fs.exists(params.filePath, (exist) => { if (!exist) { return callback(null, null); } fs.readFile(params.filePath, 'utf8', (err, contents) => { if (err) { callback(err, null); return; } let linter = new Linter(params.filePath, contents, params.options); let result = linter.lint(); result.output = result.output.split('\n').reduce((memo: string[], line: string) => { if (line !== '') { memo.push(line + '\n'); } return memo; }, []).join(''); callback(err, result); }); }); });
function instrument(tscPath: string, prepareCode: string, cleanupCode = "") { const bak = `${tscPath}.bak`; fs.exists(bak, (backupExists: boolean) => { let filename = tscPath; if (backupExists) { filename = bak; } fs.readFile(filename, "utf-8", (err: any, tscContent: string) => { if (err) throw err; fs.writeFile(bak, tscContent, (err: any) => { if (err) throw err; fs.readFile(path.resolve(path.dirname(tscPath) + "/loggedIO.js"), "utf-8", (err: any, loggerContent: string) => { if (err) throw err; const invocationLine = "ts.executeCommandLine(ts.sys.args);"; const index1 = tscContent.indexOf(invocationLine); if (index1 < 0) { throw new Error(`Could not find ${invocationLine}`); } const index2 = index1 + invocationLine.length; const newContent = tscContent.substr(0, index1) + loggerContent + prepareCode + invocationLine + cleanupCode + tscContent.substr(index2) + "\r\n"; fs.writeFile(tscPath, newContent); }); }); }); }); }
export function removeFilesFromDir(dir:string, callback:AsyncCallback) { dir = path.resolve(dir); fs.exists(dir, (exists:bool) => { if (!exists) return callback('path does not exists: ' + dir, null); async.waterfall([ (callback:AsyncCallback) => { return fs.stat(dir, callback); }, (stats, callback:AsyncCallback) => { if (!stats.isDirectory()) { return callback('path is not a directory: ' + dir, null); } return fs.readdir(dir, callback); }, (files:string[], callback:AsyncCallback) => { //check each file async.forEach(files, (file:string, callback:AsyncCallback) => { var full = path.join(dir, file); fs.stat(full, (err, stats)=> { if (err) return callback(err, null); if (stats.isFile()) { return fs.unlink(full, callback); } return callback(null, null); }) }, callback); } ], callback); }); }
export function verifyUser (req, res, next) { const fp = getUserFilePath(req.params.username); fs.exists(fp, yes => { if(yes) next(); else res.redirect('/error/' + req.params.username); }); };
form.parse(req, (err, fields:Fields, files:Files)=> { if (!files) { res.end(JSON.stringify({ok: false, error: "no_file_data"})); } var file = files["file"]; var tmpPath = file.path; var subDir = makeSubpath(token); var newDir = '/thumbs/' + subDir; var newPath = newDir + "/" + jpgFile; fs.exists(newDir, (exists:boolean)=> { if (!exists) { // console.log("Folder " + newDir + " not exist"); mkdirp(newDir, (err)=> { if (err) { res.end(JSON.stringify({ok: false, error: "cant_create_subdir"})); console.error(err); } else { moveFile(tmpPath, newPath); } }); } else { moveFile(tmpPath, newPath); } }); res.writeHead(200, {'content-type': 'text/plain'}); res.end(JSON.stringify({ok: true, url: subDir + "/" + jpgFile})); });
export function exists(path:string) { var d = Q.defer() fs.exists(path, function(exists) { d.resolve(exists) }) return d.promise }
/** * serverRequestHandler * * @param serverRequest * @param serverResponse */ private serverRequestHandler(serverRequest : Http.ServerRequest, serverResponse : Http.ServerResponse) : void { console.log("Receive an HTTP request"); let clientFilePath : string = Path.join(__dirname, "../../resources/public"); let requestPath : string = Url.parse(serverRequest.url).pathname; let filePath : string; if (requestPath === "/") { filePath = Path.join(clientFilePath, "/index.html"); } else { filePath = Path.join(clientFilePath, requestPath); } console.log("Static file requested : ", filePath); FileSystem.exists(filePath, exist => { if (exist) { let readStream : FileSystem.ReadStream = FileSystem.createReadStream(filePath); serverResponse.writeHead(200); readStream.pipe(serverResponse); } else { serverResponse.writeHead(404, { "Content-Type": "text/plain" }); serverResponse.write("404 Not Found\n"); serverResponse.end(); } }); }
private static file_exists(filepath:string):Promise { var fs = require('fs'), def = when.defer() fs.exists(filepath, (exists)=> { def.resolve(exists) }) return def.promise }
UploadSummaryCommand.prototype.runCommandAsync = function () { var _this = this; var filename = this.command.message; if (!filename) { return Q(null); } var deferred = Q.defer(); fs.exists(filename, function (exists) { if (!exists) { deferred.resolve(null); } var projectId = _this.executionContext.variables[ctxm.WellKnownVariables.projectId]; var type = "DistributedTask.Core.Summary"; var name = "CustomMarkDownSummary-" + path.basename(filename); var hubName = _this.executionContext.jobInfo.description; var webapi = _this.executionContext.getWebApi(); var taskClient = webapi.getQTaskApi(); fs.stat(filename, function (err, stats) { if (err) { deferred.reject(err); } else { var headers = {}; headers["Content-Length"] = stats.size; var stream = fs.createReadStream(filename); taskClient.createAttachment(headers, stream, projectId, hubName, _this.executionContext.jobInfo.planId, _this.executionContext.jobInfo.timelineId, _this.executionContext.recordId, type, name).then(function () { return deferred.resolve(null); }, function (err) { return deferred.reject(err); }); } }); }); return deferred.promise; };
function instrument(tscPath: string, prepareCode: string, cleanupCode: string = '') { var bak = tscPath + '.bak'; fs.exists(bak, (backupExists: boolean) => { var filename = tscPath; if (backupExists) { filename = bak; } fs.readFile(filename, 'utf-8', (err: any, tscContent: string) => { if (err) throw err; fs.writeFile(bak, tscContent, (err: any) => { if (err) throw err; fs.readFile(path.resolve(path.dirname(tscPath) + '/loggedIO.js'), 'utf-8', (err: any, loggerContent: string) => { if (err) throw err; var invocationLine = 'ts.executeCommandLine(sys.args);'; var index1 = tscContent.indexOf(invocationLine); var index2 = index1 + invocationLine.length; var newContent = tscContent.substr(0, index1) + loggerContent + prepareCode + invocationLine + cleanupCode + tscContent.substr(index2) + '\r\n'; fs.writeFile(tscPath, newContent); }); }); }); }); }