|
| 1 | +var fs = require('fs'); |
| 2 | +var path = require('path'); |
| 3 | + |
| 4 | +toString(path.join(__dirname, 'lib.d.ts'), path.join(__dirname, 'lib-ts.js')); |
| 5 | +toString(path.join(__dirname, 'lib.es6.d.ts'), path.join(__dirname, 'lib-es6-ts.js')); |
| 6 | + |
| 7 | +function toString(source, dest) { |
| 8 | + |
| 9 | + var contents = fs.readFileSync(source).toString(); |
| 10 | + |
| 11 | + fs.writeFileSync(dest, |
| 12 | +`/*--------------------------------------------------------------------------------------------- |
| 13 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 14 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 15 | + *--------------------------------------------------------------------------------------------*/ |
| 16 | +
|
| 17 | +// This is a generated file from ${path.basename(source)} |
| 18 | +
|
| 19 | +define([], { contents: "${escapeText(contents)}"}); |
| 20 | +
|
| 21 | +`) |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Escape text such that it can be used in a javascript string enclosed by double quotes (") |
| 26 | + */ |
| 27 | +function escapeText(text) { |
| 28 | + // http://www.javascriptkit.com/jsref/escapesequence.shtml |
| 29 | + // \b Backspace. |
| 30 | + // \f Form feed. |
| 31 | + // \n Newline. |
| 32 | + // \O Nul character. |
| 33 | + // \r Carriage return. |
| 34 | + // \t Horizontal tab. |
| 35 | + // \v Vertical tab. |
| 36 | + // \' Single quote or apostrophe. |
| 37 | + // \" Double quote. |
| 38 | + // \\ Backslash. |
| 39 | + // \ddd The Latin-1 character specified by the three octal digits between 0 and 377. ie, copyright symbol is \251. |
| 40 | + // \xdd The Latin-1 character specified by the two hexadecimal digits dd between 00 and FF. ie, copyright symbol is \xA9. |
| 41 | + // \udddd The Unicode character specified by the four hexadecimal digits dddd. ie, copyright symbol is \u00A9. |
| 42 | + var _backspace = '\b'.charCodeAt(0); |
| 43 | + var _formFeed = '\f'.charCodeAt(0); |
| 44 | + var _newLine = '\n'.charCodeAt(0); |
| 45 | + var _nullChar = 0; |
| 46 | + var _carriageReturn = '\r'.charCodeAt(0); |
| 47 | + var _tab = '\t'.charCodeAt(0); |
| 48 | + var _verticalTab = '\v'.charCodeAt(0); |
| 49 | + var _backslash = '\\'.charCodeAt(0); |
| 50 | + var _doubleQuote = '"'.charCodeAt(0); |
| 51 | + |
| 52 | + var startPos = 0, chrCode, replaceWith = null, resultPieces = []; |
| 53 | + |
| 54 | + for (var i = 0, len = text.length; i < len; i++) { |
| 55 | + chrCode = text.charCodeAt(i); |
| 56 | + switch (chrCode) { |
| 57 | + case _backspace: |
| 58 | + replaceWith = '\\b'; |
| 59 | + break; |
| 60 | + case _formFeed: |
| 61 | + replaceWith = '\\f'; |
| 62 | + break; |
| 63 | + case _newLine: |
| 64 | + replaceWith = '\\n'; |
| 65 | + break; |
| 66 | + case _nullChar: |
| 67 | + replaceWith = '\\0'; |
| 68 | + break; |
| 69 | + case _carriageReturn: |
| 70 | + replaceWith = '\\r'; |
| 71 | + break; |
| 72 | + case _tab: |
| 73 | + replaceWith = '\\t'; |
| 74 | + break; |
| 75 | + case _verticalTab: |
| 76 | + replaceWith = '\\v'; |
| 77 | + break; |
| 78 | + case _backslash: |
| 79 | + replaceWith = '\\\\'; |
| 80 | + break; |
| 81 | + case _doubleQuote: |
| 82 | + replaceWith = '\\"'; |
| 83 | + break; |
| 84 | + } |
| 85 | + if (replaceWith !== null) { |
| 86 | + resultPieces.push(text.substring(startPos, i)); |
| 87 | + resultPieces.push(replaceWith); |
| 88 | + startPos = i + 1; |
| 89 | + replaceWith = null; |
| 90 | + } |
| 91 | + } |
| 92 | + resultPieces.push(text.substring(startPos, len)); |
| 93 | + return resultPieces.join(''); |
| 94 | +} |
0 commit comments