From 952c32250c9a24e0d1178143d8c603a23ab6c7ee Mon Sep 17 00:00:00 2001 From: Oleg Smetanin Date: Fri, 26 Oct 2018 08:36:57 +0300 Subject: [PATCH] ts_library problem https://github.com/bazelbuild/rules_typescript/issues/263 --- BUILD.bazel | 18 ++++++++++++++ dist/BUILD.bazel | 58 +++++++++++++++++++++++++++++++++++++++++++ dist/copy.js | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ dist/copy.ts | 3 +++ package.json | 6 ++++- web/BUILD.bazel | 16 ++++++++++-- web/copy.js | 42 +++++++++++++++++++++++++++++++ web/index.tsx | 2 +- 8 files changed, 205 insertions(+), 4 deletions(-) create mode 100644 dist/BUILD.bazel create mode 100644 dist/copy.js create mode 100644 dist/copy.ts create mode 100644 web/copy.js diff --git a/BUILD.bazel b/BUILD.bazel index f6db774..abb7772 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -4,4 +4,22 @@ exports_files([ "tsconfig.build.json", ]) +filegroup( + name = "node_modules", + srcs = glob( + include = ["node_modules/**/*"], + exclude = [ + # Files under test & docs may contain file names that + # are not legal Bazel labels (e.g., + # node_modules/ecstatic/test/public/中文/檔案.html) + "node_modules/test/**", + "node_modules/docs/**", + # Files with spaces are not allowed in Bazel runfiles + # See https://github.com/bazelbuild/bazel/issues/4327 + "node_modules/**/* */**", + "node_modules/**/* *", + ], + ), +) + # gazelle proto:disable_global \ No newline at end of file diff --git a/dist/BUILD.bazel b/dist/BUILD.bazel new file mode 100644 index 0000000..afe628f --- /dev/null +++ b/dist/BUILD.bazel @@ -0,0 +1,58 @@ +package(default_visibility = ["//visibility:public"]) + +load("@build_bazel_rules_nodejs//:defs.bzl", "nodejs_binary") + +# nodejs_binary( +# name = "web", +# visibility = ["//visibility:public"], +# entry_point = "gobazel/build/copy.js", +# data = [ +# ":copy.js", +# "//web:copy" +# ] +# ) + +nodejs_binary( + name = "copy", + visibility = ["//visibility:public"], + # entry_point = "gobazel/node_modules/ts-node/dist/bin.js", + entry_point = "gobazel/build/copy.js", + install_source_map_support = False, + # node_modules = "//:node_modules", + + # node_modules = "@npm//:node_modules" + data = [ + ":copy.js" + # "//:tsconfig.build.json", + ], +) + + +genrule( + name = "build", + srcs = [ + # "//web:copy", + ":copy.js", + "//web:lib", + "//backend/cmd/service:service" + # "//publicapi:publicapi" + + ], + outs = ["service", "frontend"], + cmd = "node dist/copy.js $(location //backend/cmd/service:service) $(location //web:lib) $(@D)", + # cmd = "echo \"hello\"; pwd; echo $(location tsnode); $(location tsnode) $(location copy.ts)", + # cmd = "$(location tsnode) $(location copy.ts) $(location web)", + # cmd = "$(location babel) src --out-dir $@", + tools = [":copy"], +) + + +genrule( + name = "ls", + srcs = [ + "//web:lib", + ], + outs = ['qwe'], + + cmd = "ls `dirname $(locations //web:lib)`; ls `dirname $(location //web:lib)`; echo qwe > $(location qwe)", +) diff --git a/dist/copy.js b/dist/copy.js new file mode 100644 index 0000000..efee506 --- /dev/null +++ b/dist/copy.js @@ -0,0 +1,64 @@ + +// console.log(process.env) +console.log(process.argv) + + + +const path = require('path'); +const fs = require('fs'); +const shell = require('shelljs'); + +// function readFile(srcPath) { +// return new Promise(function (resolve, reject) { +// fs.readFile(srcPath, 'utf8', function (err, data) { +// if (err) { +// reject(err) +// } else { +// resolve(data); +// } +// }); +// }) +// } + +// function writeFile(savPath, data) { +// return new Promise(function (resolve, reject) { +// fs.writeFile(savPath, data, function (err) { +// if (err) { +// reject(err) +// } else { +// resolve(); +// } +// }); +// }) +// } + +// const wsDir = process.env.BUILD_WORKSPACE_DIRECTORY; + + + +// const in_file = process.argv[2] +// const out_file = process.argv[3] +const dest = process.argv[process.argv.length-1] + +// console.log('Read ', in_file) +console.log('pwd:', process.cwd()) +console.log(shell.ls(process.cwd())) + +console.log('Write ', dest) + +shell.mkdir('-p', path.join(dest, 'frontend')) +shell.cp(process.argv[2], dest) +console.log('front path', path.dirname(process.argv[3])) +console.log(shell.ls('-R', path.resolve(path.dirname(process.argv[3]), '../' ))) +shell.cp(path.dirname(process.argv[3]), path.join(dest, 'frontend')) +// shell.cp(process.argv[3], path.join(dest, 'frontend')) + + + +// readFile(in_file) +// .then(function(results){ +// results+=" test manipulation"; +// return writeFile(path.join(out_file, 'qwe.txt'), results); +// }).then(function(){ +// //done writing file, can do other things +// }) diff --git a/dist/copy.ts b/dist/copy.ts new file mode 100644 index 0000000..f024761 --- /dev/null +++ b/dist/copy.ts @@ -0,0 +1,3 @@ + +console.log('TS!!!!') +console.log(process.argv) \ No newline at end of file diff --git a/package.json b/package.json index fde4383..35b5de2 100644 --- a/package.json +++ b/package.json @@ -4,12 +4,16 @@ "build-frontend": "bazel build //web:devserver", "start": "npm run build-frontend && npm run build-backend && concurrently \"npm run start-frontend\" \"npm run start-backend\"", "start-backend": "ibazel run //backend/cmd/service:service", - "start-frontend": "ibazel run //web:devserver" + "start-frontend": "ibazel run //web:devserver", + "start-dist": "ibazel build //dist:build" }, "dependencies": { "@bazel/ibazel": "0.4.0", "@bazel/typescript": "0.20.3", + "@types/node": "10.12.0", "concurrently": "3.5.1", + "shelljs": "0.8.2", + "ts-node": "7.0.1", "tsutils": "3.2.0", "typescript": "3.1.1" } diff --git a/web/BUILD.bazel b/web/BUILD.bazel index fe74fac..af408a5 100644 --- a/web/BUILD.bazel +++ b/web/BUILD.bazel @@ -3,7 +3,7 @@ package(default_visibility = ["//visibility:public"]) load("@build_bazel_rules_typescript//:defs.bzl", "ts_library") ts_library( - name = "src", + name = "lib", srcs = ["index.tsx"], tsconfig = "//:tsconfig.build.json", deps = [ @@ -25,10 +25,22 @@ ts_devserver( port = 9999, serving_path = "/bundle.min.js", #entry_module = "gobazel/src/frontend/main", - deps = [":src"], + deps = [":lib"], static_files = [ ":index.html", "@npm//node_modules/react:umd/react.development.js", "@npm//node_modules/react-dom:umd/react-dom.development.js" ], ) + +load("@build_bazel_rules_nodejs//:defs.bzl", "nodejs_binary") + +nodejs_binary( + name = "copy", + visibility = ["//visibility:public"], + entry_point = "gobazel/web/copy.js", + data = [ + ":copy.js", + ":index.html" + ] +) diff --git a/web/copy.js b/web/copy.js new file mode 100644 index 0000000..f6e1996 --- /dev/null +++ b/web/copy.js @@ -0,0 +1,42 @@ + +// console.log(process.env) +// console.log(process.argv) + +const path = require('path'); +const fs = require('fs'); + +function readFile(srcPath) { + return new Promise(function (resolve, reject) { + fs.readFile(srcPath, 'utf8', function (err, data) { + if (err) { + reject(err) + } else { + resolve(data); + } + }); + }) +} + +function writeFile(savPath, data) { + return new Promise(function (resolve, reject) { + fs.writeFile(savPath, data, function (err) { + if (err) { + reject(err) + } else { + resolve(); + } + }); + }) +} + +// const wsDir = process.env.BUILD_WORKSPACE_DIRECTORY; + +console.log('Copy ', path.join(__dirname, 'index.html')) + +readFile(path.join(__dirname, 'index.html')) + .then(function(results){ + results+=" test manipulation"; + return writeFile(path.join(__dirname, 'index1.html'),results); + }).then(function(){ + //done writing file, can do other things + }) diff --git a/web/index.tsx b/web/index.tsx index 17932a9..f8e7eef 100644 --- a/web/index.tsx +++ b/web/index.tsx @@ -10,7 +10,7 @@ let user: IUser = { ReactDOM.render(
-
This is app
+
This is app!!!!!!
{JSON.stringify(user)}
,