From e0de45950fe34f57cc44b5d9e33b46becc22b71d Mon Sep 17 00:00:00 2001 From: Joseph Garrone Date: Thu, 14 May 2020 01:17:48 +0200 Subject: [PATCH] initial commit --- TEMPLATE_README.md | 46 ++++++++++++++++++++++++++++++++----- package.json | 4 ++-- src/tools/getProjectRoot.ts | 23 +++++++++++++++++++ 3 files changed, 65 insertions(+), 8 deletions(-) create mode 100644 src/tools/getProjectRoot.ts diff --git a/TEMPLATE_README.md b/TEMPLATE_README.md index a2b99d5..00b14e8 100644 --- a/TEMPLATE_README.md +++ b/TEMPLATE_README.md @@ -1,3 +1,11 @@ +# Template features: + +[ts-ci](https://github.com/garronej/ts_ci) is a template that: +- Automatically fills the paperwork for you: Fills the package.json and README.md +- Automate testing: Every commit pushed will be automatically tested on docker containers against many Node and Deno version ( ``npm test`` ), if everything passes you'll get a green label on the readme. +- Publish for you on NPM: Each time you'll change the version number in ``package.json`` a workflow that publishes for NPM and [deno.land](https://deno.land/x/) will trigger. The CHANGELOG.md will be automatically updated based on commit messages since last release. +- Enable you to only track sources on the main branch: With this template you won't have to track ``dist/`` on your main branch. +- Enable short import path: No more ``import 'my_module/dist/theFileNeeded'`` your users will be able to ``import 'my_module/theFileNeeded'``. # GitHub Repo Secrets to set: @@ -6,10 +14,36 @@ The following Secrets need to be set to be able to publish. - ``PAT``: GitHub Personal access token. - ``NODE_AUTH_TOKEN``: NPM Authorization token. +# NOTES: + +- The dev dependency ``evt`` is just used as an utility in the demo ``/src/test/`` directory. +- The dev dependency ``denoify`` is needed for the NPM script ``enable_short_import_path``. + +# Warning + +The drawback of having short import path is that the file structure +is not exactly the same in production ( in the npm bundle ) and in development. + +The files and directories in ``dist/`` will be moved to the root of the project. + +As a result this won't work in production: + +``src/index.ts`` +```typescript +const buff = require("fs").readFileSync( + require("path").join(__filename,"..", "package.json") +); +``` + +Because ``/dist/index.js`` will be moved to ``/index.js`` + +You'll have to do: + +``src/index.ts`` +```typescript +import { getProjectRoot } from "./tools/getProjectRoot"; +const buff = require("fs").readFileSync( + require("path").join(getProjectRoot(),"package.json") +); +``` -[ts-ci](https://github.com/garronej/ts_ci) is a template that: -- Automatically fills the paperwork for you: Fills the package.json and README.md -- Automate testing: Every commit pushed will be automatically tested on docker containers against many Node and Deno version ( ``npm test`` ), if everything passes you'll get a green label on the readme. -- Publish for you on NPM: Each time you'll change the version number in ``package.json`` a workflow that publishes for NPM and [deno.land](https://deno.land/x/) will trigger. The CHANGELOG.md will be automatically updated based on commit messages since last release. -- Enable you to only track sources on the main branch: With this template you won't have to track ``dist/`` on your main branch. -- Enable short import path: No more ``import 'my_module/dist/theFileNeeded'`` your users will be able to ``import 'my_module/theFileNeeded'``. diff --git a/package.json b/package.json index 19c9d5e..239fabf 100755 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "test:node": "node ./dist/test", "test:deno": "deno ./deno_dist/test/deno_index.ts", "test": "npm run test:node && npm run test:deno", - "enable_short_import_path:npm": "npm run build && npx denoify_enable_short_npm_import_path", + "enable_short_import_path": "npm run build && npx denoify_enable_short_npm_import_path", "clean": "rm -rf node_modules dist" }, "author": "u/#{USER_OR_ORG}#", @@ -28,7 +28,7 @@ "devDependencies": { "typescript": "^3.9.0", "@types/node": "^10.0.0", - "denoify": "github:garronej/denoify", + "denoify": "^0.2.0", "evt": "^1.6.8" } } diff --git a/src/tools/getProjectRoot.ts b/src/tools/getProjectRoot.ts new file mode 100644 index 0000000..7de2cf6 --- /dev/null +++ b/src/tools/getProjectRoot.ts @@ -0,0 +1,23 @@ + + +import * as fs from "fs"; +import * as path from "path"; + +function getProjectRootRec(dirPath: string){ + if( fs.existSync(path.join(dirPath, "package.json")) ){ + return dirPath; + } + return getProjectRootRec(path.join(dirPath, "..")) +} + +let result: string | undefined = undefined; + +export function getProjectRoot(): string{ + + if( result !== undefined ){ + return result; + } + + return result = getProjectRootRec(__dirname); + +}