Allow writing test files in typescript

Change-type: patch
Signed-off-by: Cameron Diver <cameron@balena.io>
This commit is contained in:
Cameron Diver 2019-06-13 10:54:39 +01:00
parent 79762c2e08
commit 9dab00d156
No known key found for this signature in database
GPG Key ID: 49690ED87032539F
8 changed files with 104 additions and 57 deletions

1
.gitignore vendored
View File

@ -16,3 +16,4 @@ test/data/led_file
/coverage/
report.xml
.DS_Store
.tsbuildinfo

View File

@ -50,7 +50,7 @@ COPY package.json package-lock.json /usr/src/app/
RUN JOBS=MAX npm ci --no-optional --unsafe-perm || JOBS=MAX npm install --no-optional --unsafe-perm
COPY webpack.config.js fix-jsonstream.js hardcode-migrations.js tsconfig.json /usr/src/app/
COPY webpack.config.js fix-jsonstream.js hardcode-migrations.js tsconfig.json tsconfig.release.json /usr/src/app/
COPY src /usr/src/app/src
COPY test /usr/src/app/test
COPY typings /usr/src/app/typings

View File

@ -50,7 +50,7 @@ fi
COPY src src/
COPY typings typings/
COPY tsconfig.json hardcode-migrations.js fix-jsonstream.js ./
COPY tsconfig.json tsconfig.release.json hardcode-migrations.js fix-jsonstream.js ./
RUN npm run build:debug
RUN mkdir -p dist && echo "require('../build/app.js')" > dist/app.js

12
package-lock.json generated
View File

@ -171,6 +171,12 @@
"integrity": "sha512-FhlMa34NHp9K5MY1Uz8yb+ZvuX0pnvn3jScRSNAb75KHGB8d3rEU6hqMs3Z2vjuytcMfRg6c5CHMc3wtYyD2/A==",
"dev": true
},
"@types/chai": {
"version": "4.1.7",
"resolved": "https://registry.npmjs.org/@types/chai/-/chai-4.1.7.tgz",
"integrity": "sha512-2Y8uPt0/jwjhQ6EiluT0XCri1Dbplr0ZxfFXUz+ye13gaqE8u5gL5ppao1JrUYr9cIip5S6MvQzBS7Kke7U9VA==",
"dev": true
},
"@types/common-tags": {
"version": "1.8.0",
"resolved": "https://registry.npmjs.org/@types/common-tags/-/common-tags-1.8.0.tgz",
@ -334,6 +340,12 @@
"@types/node": "*"
}
},
"@types/mocha": {
"version": "5.2.7",
"resolved": "https://registry.npmjs.org/@types/mocha/-/mocha-5.2.7.tgz",
"integrity": "sha512-NYrtPht0wGzhwe9+/idPaBB+TqkY9AhTvOLMkThm0IoEfLaiVQZwBwyJ5puCkO3AUCWrmcoePjp2mbFocKy4SQ==",
"dev": true
},
"@types/morgan": {
"version": "1.7.35",
"resolved": "https://registry.npmjs.org/@types/morgan/-/morgan-1.7.35.tgz",

View File

@ -10,17 +10,25 @@
"scripts": {
"start": "./entry.sh",
"build": "webpack",
"build:debug": "tsc && cp -r build/src/* build && rm -rf build/src && coffee -m -c -o build src && cp -r src/migrations build/ && cp package.json build/",
"precommit": "lint-staged",
"prettify": "prettier --config ./node_modules/resin-lint/config/.prettierrc --write \"{src,test,typings}/**/*.ts\"",
"lint:coffee": "resin-lint src/ test/",
"lint:typescript": "resin-lint --typescript src/ test/ typings/ && tsc --noEmit",
"build:debug": "npm run typescript:release && npm run coffeescript:release && npm run migrations:copy && npm run packagejson:copy",
"lint": "npm run lint:coffee && npm run lint:typescript",
"test": "npm run lint && npm run test:build && JUNIT_REPORT_PATH=report.xml istanbul cover _mocha && npm run coverage",
"test:build": "npm run typescript:test-build && npm run coffeescript:test && npm run testitems:copy && npm run migrations:copy-test && npm run packagejson:copy",
"coverage": "istanbul report text && istanbul report html",
"test:fast": "npm run test:build && mocha",
"test:build": "tsc && coffee -m -c -o build . && cp -r test/data build/test/ && cp -r src/migrations build/src && cp package.json build",
"test:debug": "npm run test:build && mocha --inspect-brk",
"coverage": "istanbul report text && istanbul report html"
"precommit": "lint-staged",
"prettify": "prettier --config ./node_modules/resin-lint/config/.prettierrc --write \"{src,test,typings}/**/*.ts\"",
"typescript:test-build": "tsc --project tsconfig.json",
"typescript:release": "tsc --project tsconfig.release.json && cp -r build/src/* build && rm -rf build/src",
"coffeescript:test": "coffee -m -c -o build .",
"coffeescript:release": "coffee -m -c -o build src",
"migrations:copy": "cp -r src/migrations build/",
"migrations:copy-test": "cp -r src/migrations build/src",
"packagejson:copy": "cp package.json build/",
"testitems:copy": "cp -r test/data build/test/",
"lint:coffee": "resin-lint src/ test/",
"lint:typescript": "resin-lint --typescript src/ test/ typings/ && tsc --noEmit"
},
"private": true,
"dependencies": {
@ -31,6 +39,7 @@
},
"devDependencies": {
"@types/bluebird": "^3.5.25",
"@types/chai": "^4.1.7",
"@types/common-tags": "^1.8.0",
"@types/dockerode": "^2.5.13",
"@types/event-stream": "^3.3.34",
@ -40,6 +49,7 @@
"@types/lodash": "4.14.122",
"@types/memoizee": "^0.4.2",
"@types/mkdirp": "^0.5.2",
"@types/mocha": "^5.2.7",
"@types/morgan": "^1.7.35",
"@types/mz": "0.0.32",
"@types/node": "^10.12.17",

View File

@ -2,21 +2,13 @@
"compilerOptions": {
"module": "commonjs",
"noImplicitAny": true,
"noUnusedParameters": true,
"noUnusedLocals": true,
"preserveConstEnums": true,
"removeComments": true,
"sourceMap": true,
"inlineSourceMap": true,
"strictNullChecks": true,
"outDir": "./build/",
"skipLibCheck": true,
"lib": [
"es6"
],
"lib": ["es6"],
"resolveJsonModule": true
},
"include": [
"src/**/*.ts",
"typings/**/*.d.ts"
]
"include": ["src/**/*.ts", "test/**/*.ts", "typings/**/*.d.ts"]
}

10
tsconfig.release.json Normal file
View File

@ -0,0 +1,10 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"noUnusedParameters": true,
"noUnusedLocals": true,
"preserveConstEnums": true,
"removeComments": true
},
"include": ["src/**/*.ts", "typings/**/*.d.ts"]
}

View File

@ -18,14 +18,14 @@ var externalModules = [
'oracledb',
'pg-query-stream',
'tedious',
/mssql\/.*/
]
/mssql\/.*/,
];
var requiredModules = []
var maybeOptionalModules = []
lookForOptionalDeps = function (sourceDir) {
var requiredModules = [];
var maybeOptionalModules = [];
lookForOptionalDeps = function(sourceDir) {
// We iterate over the node modules and mark all optional dependencies as external
var dirs = fs.readdirSync(sourceDir)
var dirs = fs.readdirSync(sourceDir);
for (let dir of dirs) {
let packageJson = {};
let internalNodeModules = path.join(sourceDir, dir, 'node_modules');
@ -33,54 +33,72 @@ lookForOptionalDeps = function (sourceDir) {
lookForOptionalDeps(internalNodeModules);
}
try {
packageJson = JSON.parse(fs.readFileSync(path.join(sourceDir, dir, '/package.json')));
}
catch (e) {
packageJson = JSON.parse(
fs.readFileSync(path.join(sourceDir, dir, '/package.json'))
);
} catch (e) {
continue;
}
if (packageJson.optionalDependencies != null){
maybeOptionalModules = maybeOptionalModules.concat(_.keys(packageJson.optionalDependencies))
if (packageJson.optionalDependencies != null) {
maybeOptionalModules = maybeOptionalModules.concat(
_.keys(packageJson.optionalDependencies)
);
}
if (packageJson.dependencies != null){
requiredModules = requiredModules.concat(_.keys(packageJson.dependencies))
if (packageJson.dependencies != null) {
requiredModules = requiredModules.concat(
_.keys(packageJson.dependencies)
);
}
}
}
};
lookForOptionalDeps('./node_modules')
externalModules.push(new RegExp('^(' + _.reject(maybeOptionalModules, requiredModules).map(_.escapeRegExp).join('|') + ')(/.*)?$'));
lookForOptionalDeps('./node_modules');
externalModules.push(
new RegExp(
'^(' +
_.reject(maybeOptionalModules, requiredModules)
.map(_.escapeRegExp)
.join('|') +
')(/.*)?$'
)
);
console.log('Using the following dependencies as external:', externalModules);
module.exports = function (env) {
module.exports = function(env) {
return {
mode: env == null || !env.noOptimize ? 'production' : 'development',
devtool: 'none',
entry: './src/app.coffee',
output: {
filename: 'app.js',
path: path.resolve(__dirname, 'dist')
path: path.resolve(__dirname, 'dist'),
},
resolve: {
extensions: [".js", ".ts", ".json", ".coffee"]
extensions: ['.js', '.ts', '.json', '.coffee'],
},
target: 'node',
node: {
__dirname: false
__dirname: false,
},
module: {
rules: [
{
test: new RegExp(_.escapeRegExp(path.join('knex', 'lib', 'migrate', 'index.js')) + '$'),
use: require.resolve('./hardcode-migrations')
test: new RegExp(
_.escapeRegExp(path.join('knex', 'lib', 'migrate', 'index.js')) +
'$'
),
use: require.resolve('./hardcode-migrations'),
},
{
test: new RegExp(_.escapeRegExp(path.join('JSONStream', 'index.js')) + '$'),
use: require.resolve('./fix-jsonstream')
test: new RegExp(
_.escapeRegExp(path.join('JSONStream', 'index.js')) + '$'
),
use: require.resolve('./fix-jsonstream'),
},
{
test: /\.coffee$/,
use: require.resolve('coffee-loader')
use: require.resolve('coffee-loader'),
},
{
test: /\.ts$/,
@ -89,36 +107,40 @@ module.exports = function (env) {
loader: 'ts-loader',
options: {
transpileOnly: true,
}
}
]
}
]
configFile: 'tsconfig.release.json',
},
},
],
},
],
},
externals: (context, request, callback) => {
for (let m of externalModules) {
if ((typeof m === 'string' && m === request) || (m instanceof RegExp && m.test(request))) {
if (
(typeof m === 'string' && m === request) ||
(m instanceof RegExp && m.test(request))
) {
return callback(null, 'commonjs ' + request);
} else if (typeof m != 'string' && !(m instanceof RegExp)) {
throw new Error('Invalid entry in external modules: ' + m);
}
}
return callback()
return callback();
},
plugins: [
new ForkTsCheckerWebpackPlugin({
async: false
async: false,
}),
new CopyWebpackPlugin([
{
from: './src/migrations',
to: 'migrations'
}
to: 'migrations',
},
]),
new webpack.ContextReplacementPlugin(
/\.\/migrations/,
path.resolve(__dirname, 'src/migrations')
),
]
],
};
}
};