本文整理汇总了TypeScript中co类的典型用法代码示例。如果您正苦于以下问题:TypeScript co类的具体用法?TypeScript co怎么用?TypeScript co使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了co类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: Promise
return new Promise((resolve, reject) => {
let comp = location.split("/");
// If the length is 1 then only the one file needs to be created
if (comp.length === 1) {
fs.writeFile(`${currentLocation}/${location}.${type}`, file,
err => reject(err),
() => resolve("Created successfully")
);
}
else {
let path = `${currentLocation}/`,
fileName = `${comp[comp.length - 1]}.${type}`;
// Add all of the other params to the path except for the last one which is the name of the file
for (let i = 0; i < comp.length - 1; i++) path += `${comp[i]}/`;
co(function* (){
yield mkdirp(path);
})
.catch(err => console.log(err.stack))
.then(() => {
fs.writeFile(`${currentLocation}/${location}.${type}`, file,
(err) => reject(err),
() => resolve("Created successfully")
)
})
}
})
示例2: createDefaultDbDocs
export function createDefaultDbDocs(db) {
co(function *() {
let users = yield db.collection('users').find({}).toArray();
// If the users array is empty
if (users.length === 0) {
console.log('No users in the database. Creating default users now...');
let users = [
{username: 'filip.lauc93@gmail.com', password: 'filip', status: 'offline', profileImage: 0},
{username: 'wojtek.kwiatek@gmail.com', password: 'wojtek', status: 'offline', profileImage: 0},
{username: 'laco0416@gmail.com', password: 'suguru', status: 'offline', profileImage: 0},
{username: 'mgualtieri7@gmail.com', password: 'mary', status: 'offline', profileImage: 0},
{username: 'ran.wahle@gmail.com', password: 'ran', status: 'offline', profileImage: 0}
];
for (let i = 0; i < users.length; i++) {
users[i].profileImage = generateRandomInt(1, 49);
users[i].password = yield hashPass(users[i].password);
}
let r = yield db.collection('users').insertMany(users);
if (r.insertedCount === users.length) console.log('...users created successfully.')
}
}).catch((err) => console.log(err))
}
示例3: Promise
return new Promise((resolve, reject) => {
if (!doInject) return resolve(value);
co(function *() {
let data;
try {
data = yield promiseFsReadFile(location)
} catch (err) {
throw err;
}
data = data.toString().split('\n');
data.splice(lineNumber, 0, items[item].types[type].html);
let full = data.join('\n');
return yield promiseFsWriteFile(location, full)
})
.then(res => resolve(value))
.catch(err => reject(err))
})
示例4: co
}).then(values => {
if (values.generateApp) {
co(function *() {
let appArray = [];
// TODO Finish flag implementation
// Files per flag
// flagsForType = {
// "standard": {
// t: [createFile(flags[values.json.appType].t, "tslint", "json")],
// g: [createFile(flags[values.json.appType].g, "gulpfile", "js")]
// },
// "npm library": {
// t: [createFile(flags[values.json.appType].t, "tslint", "json")],
// g: [createFile(flags[values.json.appType].g, "gulpfile", "js")]
// }
// };
switch (values.json.appType) {
case "standard":
appArray = [
createFile(createTemplateStringFromObject(values.json), "ng2config", "json"),
createFile(index(values.json.appFolder, values.json.bootLocation.slice(0, -3), values.json.appName), "index", "html"),
createFile(createTemplateStringFromObject(tsconfig), "tsconfig", "json"),
createFile(createTemplateStringFromObject(packageJson(values.json.appName)), "package", "json"),
createFile(createTemplateStringFromObject(typings), "typings", "json"),
createFile(boot, `${values.json.appFolder}/boot`, "ts"),
createFile(appComponent, `${values.json.appFolder}/app.component`, "ts")
];
break;
case "npm library":
break;
}
// If flags were provided create the reguired files
if (values.appFlags) values.appFlags.split("").forEach(a => flagsForType[values.appType][a].forEach(b => appArray.push(b)));
yield appArray
})
.catch(err => reject(err.stack))
.then(() => {
console.log(chalk.green(`\nApplication created. Attempting to run scripts now.`));
resolve(false)
});
}
else {
createFile(createTemplateStringFromObject(values.json), "ng2config", "json")
.catch(err => reject(err))
.then(() => {
console.log(chalk.green(`\nng2config.json created successfully.`));
resolve(true)
});
}
})
示例5: Promise
return new Promise((resolve, reject) => {
// Display the intro text
console.log(initPrompt.intro);
co(function *() {
// Match paths of this format: app/path/path
let pathValidator = /^(([A-z0-9\-]+\/)*[A-z0-9\-]+$)/g,
// Match paths of the same format that end with .ts
pathFileValidator = /^(([A-z0-9\-]+\/)*[A-z0-9\-]+(.ts)+$)/g,
ynValidator = /^([yn]|(yes)|(no))$/ig,
appName,
toReturn,
introMessage = (format) => `\nPlease enter a path matching the following format: ${chalk.green(format)}\nDon't add a leading or trailing slash to the path.\n`,
prompts = [
{
name: "appFolder",
question: "App Folder: (app) ",
value: "app",
message: "something/foo/bar",
validator: pathValidator
},
{
name: "bootFile",
question: "Location of bootstrap file: (boot.ts) ",
value: "boot.ts",
message: "something/foo/bar.ts",
validator: pathFileValidator
},
{
name: "componentsFolder",
question: "Components Folder: (common/components) ",
value: "common/components",
message: "something/foo/bar",
validator: pathValidator
},
{
name: "servicesFolder",
question: "Services Folder: (common/services) ",
value: "common/services",
message: "something/foo/bar",
validator: pathValidator
},
{
name: "directivesFolder",
question: "Directives Folder: (common/directives) ",
value: "common/directives",
message: "something/foo/bar",
validator: pathValidator
},
{
name: "pipesFolder",
question: "Pipes Folder: (common/pipes) ",
value: "common/pipes",
message: "something/foo/bar",
validator: pathValidator
}
],
values = {
appFolder: "app",
bootFile: "boot.ts",
componentsFolder: "common/components",
servicesFolder: "common/services",
directivesFolder: "common/directives",
pipesFolder: "common/pipes"
},
generateApp = (yield prompt("Create starter app? (Y/n) ")) || "Y";
while (generateApp.search(ynValidator) === -1) {
console.log(chalk.red("\nOnly Y,N,yes and no are valid inputs.\n"));
generateApp = (yield prompt("Create starter app? (Y/n) ")) || "Y";
}
generateApp = /^(y|(yes))$/ig.test(generateApp);
if (generateApp) {
let appTypeQuestion = `What kind of starting structure do you want to generate?\n(input the number associated with the app type)\n\n 1 - standard\n 2 - npm library\n\nStructure (1): `,
appNameQuestion = `App name: (test-app) `,
appType = (yield prompt(appTypeQuestion)) || "1",
appTypeName,
allowedFlagsValidator,
allowedFlags = {
"standard": [["t", "tslint"], ["g", "gulpfile"], ["a", "standard api service"], ["r", "basic routing"], ["l", "basic login"], ["s", "basic signup"]],
"npmLibrary": [["t", "tslint"], ["g", "gulpfile"]]
};
while (appType.search(/^(1|2)$/g) === -1) {
console.log(chalk.red("\nPlease provide a number between 1 and 2\n"));
appType = (yield prompt(appTypeQuestion)) || "1";
}
appTypeName = appType === "1" ? "standard" : "npmLibrary";
appName = (yield prompt(appNameQuestion)) || "test-app";
//.........这里部分代码省略.........