mirror of
https://github.com/lxsang/antos-frontend.git
synced 2025-05-19 09:21:50 +02:00
Add support to typescript in CodePad
This commit is contained in:
parent
cdc11089fa
commit
3ca7b91c37
5
Makefile
5
Makefile
@ -69,6 +69,11 @@ lite: build_javascripts build_themes build_packages
|
|||||||
ts:
|
ts:
|
||||||
-rm -rf dist
|
-rm -rf dist
|
||||||
tsc -p tsconfig.json
|
tsc -p tsconfig.json
|
||||||
|
cat `find dist/core/ -name "*.d.ts"` > /tmp/antos.d.ts
|
||||||
|
rm `find dist/ -name "*.d.ts"`
|
||||||
|
cat src/core.dts /tmp/antos.d.ts > /tmp/corelib.d.ts
|
||||||
|
-rm src/packages/CodePad/libs/corelib.d.ts.zip
|
||||||
|
zip -j src/packages/CodePad/libs/corelib.d.ts.zip /tmp/corelib.d.ts
|
||||||
|
|
||||||
standalone_tags: ts
|
standalone_tags: ts
|
||||||
@echo "$(BLUE)Bundling standalone tags files$(NC)"
|
@echo "$(BLUE)Bundling standalone tags files$(NC)"
|
||||||
|
19825
src/core.dts
Normal file
19825
src/core.dts
Normal file
File diff suppressed because it is too large
Load Diff
4
src/libs/jquery-3.2.1.min.js
vendored
4
src/libs/jquery-3.2.1.min.js
vendored
File diff suppressed because one or more lines are too long
@ -5,7 +5,7 @@ namespace OS {
|
|||||||
declare var CoffeeScript: any;
|
declare var CoffeeScript: any;
|
||||||
declare var JSZip: any;
|
declare var JSZip: any;
|
||||||
declare var Terser: any;
|
declare var Terser: any;
|
||||||
|
declare var ts: any;
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
@ -13,6 +13,17 @@ namespace OS {
|
|||||||
* @extends {App.BaseExtension}
|
* @extends {App.BaseExtension}
|
||||||
*/
|
*/
|
||||||
class AntOSDK extends App.BaseExtension {
|
class AntOSDK extends App.BaseExtension {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Core library for the transpiler stored here
|
||||||
|
*
|
||||||
|
* @static
|
||||||
|
* @type {GenericObject<any>}
|
||||||
|
* @memberof AntOSDK
|
||||||
|
*/
|
||||||
|
static corelib: GenericObject<any>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*Creates an instance of AntOSDK.
|
*Creates an instance of AntOSDK.
|
||||||
* @param {application.CodePad} app
|
* @param {application.CodePad} app
|
||||||
@ -121,7 +132,7 @@ namespace OS {
|
|||||||
|
|
||||||
// private functions
|
// private functions
|
||||||
/**
|
/**
|
||||||
*
|
* Create project template
|
||||||
*
|
*
|
||||||
* @private
|
* @private
|
||||||
* @param {string} path
|
* @param {string} path
|
||||||
@ -135,13 +146,15 @@ namespace OS {
|
|||||||
`${rpath}/javascripts`,
|
`${rpath}/javascripts`,
|
||||||
`${rpath}/css`,
|
`${rpath}/css`,
|
||||||
`${rpath}/coffees`,
|
`${rpath}/coffees`,
|
||||||
|
`${rpath}/ts`,
|
||||||
`${rpath}/assets`,
|
`${rpath}/assets`,
|
||||||
];
|
];
|
||||||
if (flag) {
|
if (flag) {
|
||||||
dirs.unshift(rpath);
|
dirs.unshift(rpath);
|
||||||
}
|
}
|
||||||
const files = [
|
const files = [
|
||||||
["templates/sdk-main.tpl", `${rpath}/coffees/main.coffee`],
|
["templates/sdk-main-coffee.tpl", `${rpath}/coffees/main.coffee`],
|
||||||
|
["templates/sdk-main-ts.tpl", `${rpath}/ts/main.ts`],
|
||||||
["templates/sdk-package.tpl", `${rpath}/package.json`],
|
["templates/sdk-package.tpl", `${rpath}/package.json`],
|
||||||
["templates/sdk-project.tpl", `${rpath}/project.json`],
|
["templates/sdk-project.tpl", `${rpath}/project.json`],
|
||||||
["templates/sdk-README.tpl", `${rpath}/README.md`],
|
["templates/sdk-README.tpl", `${rpath}/README.md`],
|
||||||
@ -169,14 +182,14 @@ namespace OS {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Check coffeescript file validity
|
||||||
*
|
*
|
||||||
* @private
|
* @private
|
||||||
* @param {string[]} list
|
* @param {string[]} list
|
||||||
* @returns {Promise<void>}
|
* @returns {Promise<void>}
|
||||||
* @memberof AntOSDK
|
* @memberof AntOSDK
|
||||||
*/
|
*/
|
||||||
private verify(list: string[]): Promise<void> {
|
private verify_coffee(list: string[]): Promise<void> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (list.length === 0) {
|
if (list.length === 0) {
|
||||||
return resolve();
|
return resolve();
|
||||||
@ -188,7 +201,7 @@ namespace OS {
|
|||||||
.then((data) => {
|
.then((data) => {
|
||||||
try {
|
try {
|
||||||
CoffeeScript.nodes(data);
|
CoffeeScript.nodes(data);
|
||||||
return this.verify(list)
|
return this.verify_coffee(list)
|
||||||
.then(() => resolve())
|
.then(() => resolve())
|
||||||
.catch((e) => reject(__e(e)));
|
.catch((e) => reject(__e(e)));
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
@ -200,25 +213,142 @@ namespace OS {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* load typescript core lib
|
||||||
*
|
*
|
||||||
|
* @private
|
||||||
|
* @param {string} path
|
||||||
|
* @return {*} {Promise<any>}
|
||||||
|
* @memberof AntOSDK
|
||||||
|
*/
|
||||||
|
private load_corelib(path: string): Promise<any> {
|
||||||
|
return new Promise(async (resolve, reject) => {
|
||||||
|
if (AntOSDK.corelib["ts"]) {
|
||||||
|
return resolve(AntOSDK.corelib["ts"]);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const code = await this.loadzip(`${path}.zip`, "text");
|
||||||
|
AntOSDK.corelib["ts"] = ts.createSourceFile(path, code, ts.ScriptTarget.Latest);
|
||||||
|
return resolve(AntOSDK.corelib["ts"]);
|
||||||
|
} catch (e) {
|
||||||
|
return reject(__e(e));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compile typescript to javascript
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
* @param {string[]} files
|
||||||
|
* @return {*} {Promise<string>}
|
||||||
|
* @memberof AntOSDK
|
||||||
|
*/
|
||||||
|
private compile_ts(files: string[]): Promise<string> {
|
||||||
|
return new Promise(async (resolve, reject) => {
|
||||||
|
if (files.length == 0) {
|
||||||
|
return resolve(undefined);
|
||||||
|
}
|
||||||
|
const core_lib = "os://packages/CodePad/libs/corelib.d.ts";
|
||||||
|
try {
|
||||||
|
await this.load_corelib(core_lib);
|
||||||
|
const arr = [];
|
||||||
|
this.read_files(files, arr).then((_result) => {
|
||||||
|
const libs: string[] = arr.map((e) => e.path)
|
||||||
|
libs.unshift(core_lib);
|
||||||
|
const src_files: GenericObject<any> = {};
|
||||||
|
src_files[core_lib] = AntOSDK.corelib["ts"];
|
||||||
|
for (const el of arr) {
|
||||||
|
src_files[el.path] = ts.createSourceFile(el.path, el.content, ts.ScriptTarget.Latest);
|
||||||
|
}
|
||||||
|
let js_code = "";
|
||||||
|
const host = {
|
||||||
|
fileExists: (path: string) => {
|
||||||
|
return src_files[path] != undefined;
|
||||||
|
},
|
||||||
|
directoryExists: (path: string) => {
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
getCurrentDirectory: () => "/",
|
||||||
|
getDirectories: () => [],
|
||||||
|
getCanonicalFileName: (path: string) => path,
|
||||||
|
getNewLine: () => "\n",
|
||||||
|
getDefaultLibFileName: () => "",
|
||||||
|
getSourceFile: (path: string) => src_files[path],
|
||||||
|
readFile: (path: string) => undefined,
|
||||||
|
useCaseSensitiveFileNames: () => true,
|
||||||
|
writeFile: (path: string, data: string) => js_code = `${js_code}\n${data}`,
|
||||||
|
};
|
||||||
|
const program = ts.createProgram(libs, {
|
||||||
|
"target": "es6",
|
||||||
|
"skipLibCheck": true,
|
||||||
|
}, host);
|
||||||
|
const result = program.emit();
|
||||||
|
const diagnostics = result.diagnostics.concat((ts.getPreEmitDiagnostics(program)));
|
||||||
|
if (diagnostics.length > 0) {
|
||||||
|
diagnostics.forEach(diagnostic => {
|
||||||
|
if (diagnostic.file) {
|
||||||
|
let { line, character } = ts.getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start!);
|
||||||
|
let message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n");
|
||||||
|
this.logger().error(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`);
|
||||||
|
} else {
|
||||||
|
this.logger().error(ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return reject(API.throwe(__("Typescript compile error")));
|
||||||
|
}
|
||||||
|
resolve(js_code);
|
||||||
|
})
|
||||||
|
.catch((e) => { reject(__e(e)) });
|
||||||
|
} catch (e) {
|
||||||
|
return reject(__e(e));
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
private compile(meta: GenericObject<any>): Promise<any> {
|
||||||
|
return new Promise(async (resolve, reject) => {
|
||||||
|
const libs = [
|
||||||
|
`${this.basedir()}/libs/terser.min.js`,
|
||||||
|
];
|
||||||
|
if (meta.coffees.length > 0) {
|
||||||
|
libs.push(`${this.basedir()}/libs/coffeescript.js`);
|
||||||
|
}
|
||||||
|
if (meta.ts.length > 0) {
|
||||||
|
libs.push("os://scripts/jszip.min.js");
|
||||||
|
libs.push(`${this.basedir()}/libs/typescript.min.js`)
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
await this.import(libs);
|
||||||
|
const coffee_list = meta.coffees.map(
|
||||||
|
(v: string) => `${meta.root.trimBy("/")}/${v}`
|
||||||
|
);
|
||||||
|
const ts_list = meta.ts.map(
|
||||||
|
(v: string) => `${meta.root.trimBy("/")}/${v}`
|
||||||
|
);
|
||||||
|
Promise.all([
|
||||||
|
this.compile_ts(ts_list),
|
||||||
|
this.compile_coffee(coffee_list)
|
||||||
|
]).then((js_codes: string[]) => {
|
||||||
|
resolve(js_codes.join("\n"));
|
||||||
|
}).catch((e_1) => reject(__e(e_1)));
|
||||||
|
} catch (e_2) {
|
||||||
|
return reject(__e(e_2));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compile coffeescript to javascript
|
||||||
*
|
*
|
||||||
* @private
|
* @private
|
||||||
* @param {GenericObject<any>} meta
|
* @param {GenericObject<any>} meta
|
||||||
* @returns {Promise<any>}
|
* @returns {Promise<string>}
|
||||||
* @memberof AntOSDK
|
* @memberof AntOSDK
|
||||||
*/
|
*/
|
||||||
private compile(meta: GenericObject<any>): Promise<any> {
|
private compile_coffee(list: string[]): Promise<string> {
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
await this.import([
|
await this.verify_coffee(list.map((x: string) => x));
|
||||||
`${this.basedir()}/libs/coffeescript.js`,
|
|
||||||
`${this.basedir()}/libs/terser.min.js`,
|
|
||||||
]);
|
|
||||||
const list = meta.coffees.map(
|
|
||||||
(v: string) => `${meta.root}/${v}`
|
|
||||||
);
|
|
||||||
try {
|
|
||||||
await this.verify(list.map((x: string) => x));
|
|
||||||
try {
|
try {
|
||||||
const code = await this.cat(list, "");
|
const code = await this.cat(list, "");
|
||||||
const jsrc = CoffeeScript.compile(code);
|
const jsrc = CoffeeScript.compile(code);
|
||||||
@ -230,14 +360,11 @@ namespace OS {
|
|||||||
} catch (e_1) {
|
} catch (e_1) {
|
||||||
return reject(__e(e_1));
|
return reject(__e(e_1));
|
||||||
}
|
}
|
||||||
} catch (e_2) {
|
|
||||||
return reject(__e(e_2));
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Build the project
|
||||||
*
|
*
|
||||||
* @private
|
* @private
|
||||||
* @param {GenericObject<any>} meta
|
* @param {GenericObject<any>} meta
|
||||||
@ -354,7 +481,7 @@ namespace OS {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Run the built project
|
||||||
*
|
*
|
||||||
* @private
|
* @private
|
||||||
* @param {GenericObject<any>} meta
|
* @param {GenericObject<any>} meta
|
||||||
@ -383,5 +510,6 @@ namespace OS {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
AntOSDK.corelib = {};
|
||||||
App.extensions.AntOSDK = AntOSDK;
|
App.extensions.AntOSDK = AntOSDK;
|
||||||
}
|
}
|
||||||
|
@ -38,6 +38,39 @@ namespace OS {
|
|||||||
return API.require(libs);
|
return API.require(libs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get content of a file compressed in a zip archive
|
||||||
|
*
|
||||||
|
* @param {*} file
|
||||||
|
* @memberof BaseExtension
|
||||||
|
*/
|
||||||
|
loadzip(file: string, type:string): Promise<any>
|
||||||
|
{
|
||||||
|
return new Promise(async (resolve, reject) =>{
|
||||||
|
try {
|
||||||
|
const data = await file.asFileHandle().read("binary");
|
||||||
|
try {
|
||||||
|
const zip = await JSZip.loadAsync(data);
|
||||||
|
let file_name = "";
|
||||||
|
for (let name in zip.files) {
|
||||||
|
file_name = name;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const udata = await zip.file(file_name).async(type);
|
||||||
|
resolve(udata);
|
||||||
|
} catch (e_2) {
|
||||||
|
return reject(__e(e_2));
|
||||||
|
}
|
||||||
|
} catch (e_1) {
|
||||||
|
return reject(__e(e_1));
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
return reject(__e(e));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
@ -82,7 +115,7 @@ namespace OS {
|
|||||||
* @return {Logger} editor logger
|
* @return {Logger} editor logger
|
||||||
* @memberof BaseExtension
|
* @memberof BaseExtension
|
||||||
*/
|
*/
|
||||||
protected logger() {
|
protected logger(): any {
|
||||||
if (!this.app.setting.showBottomBar) {
|
if (!this.app.setting.showBottomBar) {
|
||||||
this.app.showOutput(true);
|
this.app.showOutput(true);
|
||||||
}
|
}
|
||||||
@ -104,7 +137,7 @@ namespace OS {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Cat all files to a single output string
|
||||||
*
|
*
|
||||||
* @protected
|
* @protected
|
||||||
* @param {string[]} list
|
* @param {string[]} list
|
||||||
@ -130,6 +163,37 @@ namespace OS {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read all file content in the list
|
||||||
|
*
|
||||||
|
* @protected
|
||||||
|
* @param {string[]} list
|
||||||
|
* @return {*} {Promise<GenericObject<string>>}
|
||||||
|
* @memberof BaseExtension
|
||||||
|
*/
|
||||||
|
protected read_files(list:string[], contents: GenericObject<string>[]): Promise<GenericObject<string>[]>
|
||||||
|
{
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
if (list.length === 0) {
|
||||||
|
return resolve(contents);
|
||||||
|
}
|
||||||
|
const file = list.splice(0, 1)[0].asFileHandle();
|
||||||
|
return file
|
||||||
|
.read()
|
||||||
|
.then((text: string) => {
|
||||||
|
contents.push({
|
||||||
|
path: file.path,
|
||||||
|
content: text
|
||||||
|
});
|
||||||
|
return this.read_files(list, contents)
|
||||||
|
.then((d) => resolve(d))
|
||||||
|
.catch((e) => reject(__e(e)));
|
||||||
|
})
|
||||||
|
.catch((e: Error) => reject(__e(e)));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
|
BIN
src/packages/CodePad/libs/corelib.d.ts.zip
Normal file
BIN
src/packages/CodePad/libs/corelib.d.ts.zip
Normal file
Binary file not shown.
15
src/packages/CodePad/libs/typescript.min.js
vendored
Normal file
15
src/packages/CodePad/libs/typescript.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
19
src/packages/CodePad/templates/sdk-main-ts.tpl
Normal file
19
src/packages/CodePad/templates/sdk-main-ts.tpl
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
|
||||||
|
namespace OS {
|
||||||
|
|
||||||
|
export namespace application {
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @class {0}
|
||||||
|
* @extends {BaseApplication}
|
||||||
|
*/
|
||||||
|
export class {0} extends BaseApplication {
|
||||||
|
constructor(args: AppArgumentsType[]) {
|
||||||
|
super("{0}", args);
|
||||||
|
}
|
||||||
|
main(): void {
|
||||||
|
// YOUR CODE HERE
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -3,5 +3,6 @@
|
|||||||
"css": [],
|
"css": [],
|
||||||
"javascripts": [],
|
"javascripts": [],
|
||||||
"coffees": ["coffees/main.coffee"],
|
"coffees": ["coffees/main.coffee"],
|
||||||
|
"ts": [],
|
||||||
"copies": ["assets/scheme.html", "package.json", "README.md"]
|
"copies": ["assets/scheme.html", "package.json", "README.md"]
|
||||||
}
|
}
|
@ -2,10 +2,12 @@
|
|||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"target": "es6",
|
"target": "es6",
|
||||||
"skipLibCheck": true,
|
"skipLibCheck": true,
|
||||||
|
"declaration": true,
|
||||||
"lib": [
|
"lib": [
|
||||||
"es6", "dom", "es2017"
|
"es6", "dom", "es2017"
|
||||||
],
|
],
|
||||||
"outDir": "dist"
|
"outDir": "dist",
|
||||||
|
"removeComments": true
|
||||||
},
|
},
|
||||||
"include": ["src/**/*.ts"]
|
"include": ["src/**/*.ts"]
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user