项目各种包越来越多,手动维护比较蛋疼,容易遗漏,所以搞了这么个工具。
https://github.com/makeflow/inplate
Dockerfile
FROM node
# @inplate
# {{#each packageFilePaths}}
# COPY {{value}}{{padding}} /app/{{value}}
# {{/each}}
# @plate
# @end
Dockerfile.js
const Glob = require('glob');
module.exports = {
data: {
packageFilePaths: pad(
Glob.sync('**/package.json', {
ignore: '**/node_modules/**',
}),
),
},
};
function pad(values) {
let maxLength = Math.max(...values.map(value => value.length));
return values.map(value => {
return {
value,
padding: value.padEnd(maxLength).slice(value.length),
};
});
}
执行 inplate Dockerfile
后,文件就会被更新为类似:
FROM node
# @inplate
# {{#each packageFilePaths}}
# COPY {{value}}{{padding}} /app/{{value}}
# {{/each}}
# @plate
COPY package.json /app/package.json
COPY packages/foo/package.json /app/packages/foo/package.json
COPY packages/bar/package.json /app/packages/bar/package.json
# @end
CI 中可以执行 inplate Dockerfile --assert
来确保文件已经更新,当然也支持配置文件、通配符等。