project initialized
This commit is contained in:
45
scripts/copy.js
Normal file
45
scripts/copy.js
Normal file
@@ -0,0 +1,45 @@
|
||||
const { writeFileSync, copyFileSync } = require('fs')
|
||||
const { resolve } = require('path')
|
||||
const packageJson = require('../package.json')
|
||||
|
||||
main()
|
||||
|
||||
function main() {
|
||||
const projectRoot = resolve(__dirname, '..')
|
||||
const distPath = resolve(projectRoot, 'dist')
|
||||
const distPackageJson = createDistPackageJson(packageJson)
|
||||
|
||||
copyFileSync(
|
||||
resolve(projectRoot, 'README.md'),
|
||||
resolve(distPath, 'README.md')
|
||||
)
|
||||
copyFileSync(
|
||||
resolve(projectRoot, 'CHANGELOG.md'),
|
||||
resolve(distPath, 'CHANGELOG.md')
|
||||
)
|
||||
copyFileSync(
|
||||
resolve(projectRoot, 'LICENSE.md'),
|
||||
resolve(distPath, 'LICENSE.md')
|
||||
)
|
||||
copyFileSync(
|
||||
resolve(projectRoot, '.npmignore'),
|
||||
resolve(distPath, '.npmignore')
|
||||
)
|
||||
writeFileSync(resolve(distPath, 'package.json'), distPackageJson)
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {typeof packageJson} packageConfig
|
||||
* @return {string}
|
||||
*/
|
||||
function createDistPackageJson(packageConfig) {
|
||||
const {
|
||||
devDependencies,
|
||||
scripts,
|
||||
engines,
|
||||
config,
|
||||
...distPackageJson
|
||||
} = packageConfig
|
||||
|
||||
return JSON.stringify(distPackageJson, null, 2)
|
||||
}
|
||||
221
scripts/migrate.js
Normal file
221
scripts/migrate.js
Normal file
@@ -0,0 +1,221 @@
|
||||
/**
|
||||
* Migrate Typescript-library-starter from 3. -> 4.
|
||||
*/
|
||||
|
||||
const JSON5 = require('json5')
|
||||
const sortObjectByKeyNameList = require('sort-object-keys')
|
||||
const {
|
||||
writeFileSync,
|
||||
copyFileSync,
|
||||
readFileSync,
|
||||
existsSync,
|
||||
unlinkSync,
|
||||
} = require('fs')
|
||||
const { resolve, join } = require('path')
|
||||
const starterPkg = require('../package.json')
|
||||
const args = process.argv.slice(2)
|
||||
const pathToProject = args[0]
|
||||
|
||||
/**
|
||||
* @typedef {typeof projectPkg} Pkg
|
||||
*/
|
||||
|
||||
if (!pathToProject) {
|
||||
throw new Error(
|
||||
'you need provide relative path to package that uses ts-lib-starter!'
|
||||
)
|
||||
}
|
||||
|
||||
const ROOT = resolve(__dirname, '..')
|
||||
const PACKAGE_ROOT = resolve(ROOT, pathToProject)
|
||||
|
||||
main()
|
||||
|
||||
function main() {
|
||||
if (!existsSync(PACKAGE_ROOT)) {
|
||||
throw new Error(`${PACKAGE_ROOT}, doesn't exists`)
|
||||
}
|
||||
|
||||
console.log('Migration initialized 👀')
|
||||
|
||||
console.log('path to Package:', PACKAGE_ROOT)
|
||||
|
||||
updatePackageJson()
|
||||
updateTsConfig()
|
||||
updateTsLintConfig()
|
||||
updateConfigDir()
|
||||
updateScriptsDir()
|
||||
|
||||
console.log('DONE ✅')
|
||||
}
|
||||
|
||||
function updatePackageJson() {
|
||||
const libPackagePkgPath = resolve(PACKAGE_ROOT, 'package.json')
|
||||
|
||||
/**
|
||||
* @type {typeof starterPkg}
|
||||
*/
|
||||
const libPackagePkg = JSON5.parse(
|
||||
readFileSync(libPackagePkgPath, { encoding: 'utf-8' })
|
||||
)
|
||||
|
||||
/**
|
||||
* @type {typeof starterPkg}
|
||||
*/
|
||||
const updatePkg = {
|
||||
...libPackagePkg,
|
||||
main: starterPkg.main,
|
||||
engines: { ...libPackagePkg.engines, ...starterPkg.engines },
|
||||
scripts: { ...libPackagePkg.scripts, ...starterPkg.scripts },
|
||||
peerDependencies: sortObjectByKeyNameList({
|
||||
...libPackagePkg.peerDependencies,
|
||||
...starterPkg.peerDependencies,
|
||||
}),
|
||||
devDependencies: sortObjectByKeyNameList({
|
||||
...starterPkg.devDependencies,
|
||||
...libPackagePkg.devDependencies,
|
||||
}),
|
||||
}
|
||||
|
||||
removePackages(updatePkg.devDependencies)
|
||||
writePackage(updatePkg)
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {{[packageName:string]:string}} devDependencies
|
||||
*/
|
||||
function removePackages(devDependencies) {
|
||||
const depsToRemove = [
|
||||
'@types/uglifyjs-webpack-plugin',
|
||||
'@types/webpack',
|
||||
'uglifyjs-webpack-plugin',
|
||||
'webpack',
|
||||
'webpack-cli',
|
||||
// packages needed for this script
|
||||
'json5',
|
||||
'@types/json5',
|
||||
'sort-object-keys',
|
||||
]
|
||||
|
||||
depsToRemove.forEach(
|
||||
(dependencyName) => delete devDependencies[dependencyName]
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {typeof starterPkg} pkg
|
||||
*/
|
||||
function writePackage(pkg) {
|
||||
const updatedLibPkgToWrite = JSON.stringify(pkg, null, 2)
|
||||
writeFileSync(join(PACKAGE_ROOT, 'package.json'), updatedLibPkgToWrite)
|
||||
|
||||
console.log('\n updated package.json:', updatedLibPkgToWrite, '\n')
|
||||
}
|
||||
}
|
||||
|
||||
function updateTsConfig() {
|
||||
/**
|
||||
* @typedef {typeof import('../tsconfig.json')} TsConfig
|
||||
*/
|
||||
|
||||
const starterConfigPath = resolve(ROOT, 'tsconfig.json')
|
||||
const libPackageConfigPath = resolve(PACKAGE_ROOT, 'tsconfig.json')
|
||||
|
||||
/**
|
||||
* @type {TsConfig}
|
||||
*/
|
||||
const starterConfig = JSON5.parse(
|
||||
readFileSync(starterConfigPath, { encoding: 'utf-8' })
|
||||
)
|
||||
|
||||
/**
|
||||
* @type {TsConfig}
|
||||
*/
|
||||
const libConfig = JSON5.parse(
|
||||
readFileSync(libPackageConfigPath, { encoding: 'utf-8' })
|
||||
)
|
||||
|
||||
console.log('starter:', starterConfig)
|
||||
console.log('library:', libConfig)
|
||||
|
||||
console.log('==TS-Config:nothing updated==\n')
|
||||
}
|
||||
|
||||
function updateTsLintConfig() {
|
||||
/**
|
||||
* @typedef {typeof import('../tslint.json')} TsLintConfig
|
||||
*/
|
||||
|
||||
const starterConfigPath = resolve(ROOT, 'tslint.json')
|
||||
const libPackageConfigPath = resolve(PACKAGE_ROOT, 'tslint.json')
|
||||
|
||||
/**
|
||||
* @type {TsLintConfig}
|
||||
*/
|
||||
const starterConfig = JSON5.parse(
|
||||
readFileSync(starterConfigPath, { encoding: 'utf-8' })
|
||||
)
|
||||
|
||||
/**
|
||||
* @type {TsLintConfig}
|
||||
*/
|
||||
const libConfig = JSON5.parse(
|
||||
readFileSync(libPackageConfigPath, { encoding: 'utf-8' })
|
||||
)
|
||||
|
||||
console.log('starter:', starterConfig)
|
||||
console.log('library:', libConfig)
|
||||
|
||||
console.log('==TS-Lint:nothing updated==\n')
|
||||
}
|
||||
|
||||
function updateConfigDir() {
|
||||
const starterConfigPathDir = resolve(ROOT, 'config')
|
||||
const libPackageConfigPathDir = resolve(PACKAGE_ROOT, 'config')
|
||||
|
||||
const filesToCopy = [
|
||||
'global.d.ts',
|
||||
'helpers.js',
|
||||
'rollup.config.js',
|
||||
'tsconfig.json',
|
||||
'types.js',
|
||||
]
|
||||
const filesToRemove = ['webpack.config.js']
|
||||
|
||||
filesToCopy.forEach((file) => {
|
||||
copyFileSync(
|
||||
resolve(starterConfigPathDir, file),
|
||||
join(libPackageConfigPathDir, file)
|
||||
)
|
||||
})
|
||||
|
||||
filesToRemove.forEach((file) => {
|
||||
unlinkSync(join(libPackageConfigPathDir, file))
|
||||
})
|
||||
|
||||
console.log('==config/ updated==\n')
|
||||
}
|
||||
|
||||
function updateScriptsDir() {
|
||||
const starterScriptsPathDir = resolve(ROOT, 'scripts')
|
||||
const libPackageScriptsPathDir = resolve(PACKAGE_ROOT, 'scripts')
|
||||
|
||||
const filesToCopy = ['copy.js', 'tsconfig.json']
|
||||
/**
|
||||
* @type {string[]}
|
||||
*/
|
||||
const filesToRemove = []
|
||||
|
||||
filesToCopy.forEach((file) => {
|
||||
copyFileSync(
|
||||
resolve(starterScriptsPathDir, file),
|
||||
join(libPackageScriptsPathDir, file)
|
||||
)
|
||||
})
|
||||
|
||||
filesToRemove.forEach((file) => {
|
||||
unlinkSync(join(libPackageScriptsPathDir, file))
|
||||
})
|
||||
|
||||
console.log('==scripts/ updated==\n')
|
||||
}
|
||||
8
scripts/tsconfig.json
Normal file
8
scripts/tsconfig.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"extends": "../config/tsconfig.json",
|
||||
"compilerOptions": {},
|
||||
"include": [
|
||||
".",
|
||||
"../config/global.d.ts"
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user