Adds project init

This commit is contained in:
Tingan Ho
2015-07-27 19:52:57 +08:00
parent e3d3cc920f
commit 38f4c2dc8d
8 changed files with 422 additions and 89 deletions

View File

@@ -30,6 +30,11 @@ namespace ts {
type: "boolean",
description: Diagnostics.Print_this_message,
},
{
name: "init",
type: "boolean",
description: Diagnostics.Initializes_a_TypeScript_project_and_creates_a_tsconfig_json_file,
},
{
name: "inlineSourceMap",
type: "boolean",
@@ -221,19 +226,36 @@ namespace ts {
}
];
export function parseCommandLine(commandLine: string[]): ParsedCommandLine {
let options: CompilerOptions = {};
let fileNames: string[] = [];
let errors: Diagnostic[] = [];
let shortOptionNames: Map<string> = {};
let optionNameMap: Map<CommandLineOption> = {};
export interface OptionNameMap {
optionNameMap: Map<CommandLineOption>;
shortOptionNames: Map<string>;
}
let optionNameMapCache: OptionNameMap;
export function getOptionNameMap(): OptionNameMap {
if (optionNameMapCache) {
return optionNameMapCache;
}
let optionNameMap: Map<CommandLineOption> = {};
let shortOptionNames: Map<string> = {};
forEach(optionDeclarations, option => {
optionNameMap[option.name.toLowerCase()] = option;
if (option.shortName) {
shortOptionNames[option.shortName] = option.name;
}
});
optionNameMapCache = { optionNameMap, shortOptionNames };
return optionNameMapCache;
}
export function parseCommandLine(commandLine: string[]): ParsedCommandLine {
let options: CompilerOptions = {};
let fileNames: string[] = [];
let errors: Diagnostic[] = [];
let { optionNameMap, shortOptionNames } = getOptionNameMap();
parseStrings(commandLine);
return {
options,
@@ -345,6 +367,107 @@ namespace ts {
return parseConfigFileText(fileName, text);
}
/**
* Remove whitespace, comments and trailing commas from JSON text.
* @param text JSON text string.
*/
function stripJsonTrivia(text: string): string {
let ch: number;
let pos = 0;
let end = text.length - 1;
let result = '';
let pendingCommaInsertion = false;
while (pos <= end) {
ch = text.charCodeAt(pos);
if(isWhiteSpace(ch) || isLineBreak(ch)) {
pos++;
continue;
}
if(ch === CharacterCodes.slash) {
if (text.charCodeAt(pos + 1) === CharacterCodes.slash) {
pos += 2;
while (pos <= end) {
if (isLineBreak(text.charCodeAt(pos))) {
break;
}
pos++;
}
continue;
}
else if (text.charCodeAt(pos + 1) === CharacterCodes.asterisk) {
pos += 2;
while (pos <= end) {
ch = text.charCodeAt(pos);
if (ch === CharacterCodes.asterisk &&
text.charCodeAt(pos + 1) === CharacterCodes.slash) {
pos += 2;
break;
}
pos++;
}
continue;
}
}
if (pendingCommaInsertion) {
if (ch !== CharacterCodes.closeBracket &&
ch !== CharacterCodes.closeBrace) {
result += ',';
}
pendingCommaInsertion = false;
}
switch (ch) {
case CharacterCodes.comma:
pendingCommaInsertion = true;
break;
case CharacterCodes.doubleQuote:
result += text[pos];
pos++;
while (pos <= end) {
ch = text.charCodeAt(pos);
if (ch === CharacterCodes.backslash) {
switch (text.charCodeAt(pos + 1)) {
case CharacterCodes.doubleQuote:
result += "\\\"";
pos += 2;
continue;
case CharacterCodes.backslash:
result += "\\\\";
pos += 2;
continue;
}
pos++;
}
result += text[pos];
if (ch === CharacterCodes.doubleQuote) {
break;
}
pos++;
}
break;
default:
result += text[pos];
}
pos++;
}
return result;
}
/**
* Parse the text of the tsconfig.json file
* @param fileName The path to the config file
@@ -352,6 +475,7 @@ namespace ts {
*/
export function parseConfigFileText(fileName: string, jsonText: string): { config?: any; error?: Diagnostic } {
try {
jsonText = stripJsonTrivia(jsonText);
return { config: /\S/.test(jsonText) ? JSON.parse(jsonText) : {} };
}
catch (e) {
@@ -425,7 +549,7 @@ namespace ts {
}
else {
let exclude = json["exclude"] instanceof Array ? map(<string[]>json["exclude"], normalizeSlashes) : undefined;
let sysFiles = host.readDirectory(basePath, ".ts", exclude).concat(host.readDirectory(basePath, ".tsx", exclude));
let sysFiles = host.readDirectory(basePath, ".ts", exclude).concat(host.readDirectory(basePath, ".tsx", exclude));
for (let i = 0; i < sysFiles.length; i++) {
let name = sysFiles[i];
if (fileExtensionIs(name, ".d.ts")) {