forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialize.ts
53 lines (44 loc) · 1.55 KB
/
serialize.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
// tslint:disable:no-implicit-dependencies
import { logging, strings } from '@angular-devkit/core';
import { SchemaClassFactory } from '@ngtools/json-schema';
import * as fs from 'fs';
export function buildSchema(inFile: string, mimetype: string): string {
const jsonSchema = JSON.parse(fs.readFileSync(inFile, 'utf-8'));
const SchemaClass = SchemaClassFactory(jsonSchema);
const schemaInstance = new SchemaClass({});
let name = inFile.split(/[\/\\]/g).pop();
if (name) {
name = strings.classify(name.replace(/\.[^.]*$/, ''));
}
const license = `/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
`.replace(/^ {4}/gm, '');
return license + schemaInstance.$$serialize(mimetype, name);
}
export default function(opts: { _: string[], mimetype?: string }, logger: logging.Logger) {
const inFile = opts._[0] as string;
const outFile = opts._[1] as string;
const mimetype = opts.mimetype || 'text/x.dts';
if (!inFile) {
logger.fatal('Command serialize needs an input file.');
} else {
const output = buildSchema(inFile, mimetype);
if (outFile) {
fs.writeFileSync(outFile, output, { encoding: 'utf-8' });
} else {
logger.info(output);
}
}
}