本文整理匯總了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";
//.........這裏部分代碼省略.........