TypeScript ts-typed-json asObject示例

说明

typescript ts-typed-json asobject示例是从最受好评的开源项目中提取的实现代码,你可以参考下面示例的使用方式。

编程语言: TypeScript

命名空间/包名称: ts-typed-json

示例#1
文件: cli.ts 项目: kodeballer/neon

    action: function(options) {
      if (options.help) {
        commandUsage('version');
        return;
      }

      console.log(JSON.asObject(metadata).version);
    }

示例#2
文件: neon_new.ts 项目: kodeballer/neon

import { readFile, writeFile, mkdirs } from '../async/fs';
import { prompt } from 'inquirer';
import gitconfig from '../async/git-config';
import * as path from 'path';
import * as handlebars from 'handlebars';
import * as semver from 'semver';
import * as style from '../style';
import validateLicense = require('validate-npm-package-license');
import validateName = require('validate-npm-package-name');
import * as JSON from 'ts-typed-json';

const ROOT_DIR = path.resolve(__dirname, '..', '..');
const TEMPLATES_DIR = path.resolve(ROOT_DIR, 'templates');

const NEON_CLI_VERSION =
  JSON.asString(JSON.asObject(JSON.loadSync(path.resolve(ROOT_DIR, 'package.json'))).version);

async function compile(filename: string) {
  let source = await readFile(path.resolve(TEMPLATES_DIR, filename), {
    encoding: 'utf8'
  });
  return handlebars.compile(source, { noEscape: true });
}

const GITIGNORE_TEMPLATE = compile('.gitignore.hbs');
const CARGO_TEMPLATE     = compile('Cargo.toml.hbs');
const NPM_TEMPLATE       = compile('package.json.hbs');
const INDEXJS_TEMPLATE   = compile('index.js.hbs');
const LIBRS_TEMPLATE     = compile('lib.rs.hbs');
const README_TEMPLATE    = compile('README.md.hbs');
const BUILDRS_TEMPLATE   = compile('build.rs.hbs');

展开阅读全文