js2me-exports-post-build-script
Advanced tools
Comparing version
@@ -1,6 +0,3 @@ | ||
import { PostBuildScriptConfig } from './types.js'; | ||
/** | ||
* Выполняет действия после сборки проекта. | ||
*/ | ||
export declare const postBuildScript: ({ buildDir, rootDir, filesToCopy, srcDirName, filterExportsPathFn, patchPackageJson, onPackageVersionChanged, }: PostBuildScriptConfig) => void; | ||
export * from './post-build-script.js'; | ||
export * from './publish-script.js'; | ||
//# sourceMappingURL=index.d.ts.map |
120
index.js
@@ -1,118 +0,2 @@ | ||
import * as utils from './utils.js'; | ||
const { $, fs, path, readFile, scanDir, writeFile } = utils; | ||
const tryToFindPackageVersionChanged = (gitDiffResult) => { | ||
if (!gitDiffResult) { | ||
return null; | ||
} | ||
const lines = gitDiffResult.split('/n'); | ||
let prevVersion = null; | ||
let nextVersion = null; | ||
for (const line of lines) { | ||
if (line.includes('- "version":')) { | ||
const rawVersion = line.split('- "version":')[1]; | ||
const [, version] = rawVersion.split('"'); | ||
prevVersion = version; | ||
} | ||
if (line.includes('+ "version":')) { | ||
const rawVersion = line.split('+ "version":')[1]; | ||
const [, version] = rawVersion.split('"'); | ||
nextVersion = version; | ||
return { nextVersion, prevVersion }; | ||
} | ||
} | ||
return null; | ||
}; | ||
const buildExportsMap = (targetPath, exportsMap, srcDirName, filterExportsPathFunction) => { | ||
const pathstat = fs.lstatSync(targetPath); // Получение информации о файле или директории | ||
if (pathstat.isDirectory()) { | ||
// Проверка, является ли путь директорией | ||
const subdirs = scanDir(targetPath); // Сканирование поддиректорий | ||
// Итерация по каждой поддиректории | ||
subdirs.forEach((subdir) => { | ||
// Рекурсивный вызов для каждой поддиректории | ||
buildExportsMap(`${targetPath}/${subdir}`, exportsMap, srcDirName, filterExportsPathFunction); | ||
}); | ||
} | ||
else { | ||
const extension = path.extname(targetPath); | ||
// Удаление расширения и имени исходной директории | ||
const fixedPath = targetPath | ||
.replace(extension, '') | ||
.replace(`${srcDirName}/`, ''); | ||
// Применение фильтра для исключения определенных путей | ||
if (filterExportsPathFunction?.(fixedPath, targetPath, extension)) { | ||
return; // Если путь исключен, выходим из функции | ||
} | ||
// Проверка, является ли файл TypeScript | ||
if (extension === '.ts' || extension === '.tsx') { | ||
// Обработка файла index | ||
if (fixedPath === 'index') { | ||
exportsMap[`.`] = { | ||
import: `./${fixedPath}.js`, | ||
default: `./${fixedPath}.js`, | ||
types: `./${fixedPath}.d.ts`, | ||
}; | ||
// Обработка других файлов с индексом в конце пути | ||
} | ||
else if (fixedPath.endsWith('/index')) { | ||
exportsMap[`./${fixedPath.split('/').slice(0, -1).join('/')}`] = { | ||
import: `./${fixedPath}.js`, | ||
default: `./${fixedPath}.js`, | ||
types: `./${fixedPath}.d.ts`, | ||
}; | ||
} | ||
else { | ||
exportsMap[`./${fixedPath}`] = { | ||
import: `./${fixedPath}.js`, | ||
default: `./${fixedPath}.js`, | ||
types: `./${fixedPath}.d.ts`, | ||
}; | ||
} | ||
} | ||
else { | ||
exportsMap[`./${fixedPath}`] = `./${fixedPath}${extension}`; | ||
} | ||
} | ||
return exportsMap; | ||
}; | ||
const defaultFilterExportsPathFunction = (path) => path.endsWith('.store') || | ||
path.endsWith('.store.types') || | ||
path.endsWith('.types') || | ||
path.endsWith('.impl'); | ||
/** | ||
* Выполняет действия после сборки проекта. | ||
*/ | ||
export const postBuildScript = ({ buildDir, rootDir = '.', filesToCopy = [], srcDirName = 'src', filterExportsPathFn = defaultFilterExportsPathFunction, patchPackageJson, onPackageVersionChanged, }) => { | ||
const packageJson = JSON.parse(readFile(`${rootDir}/package.json`).toString()); | ||
filesToCopy?.forEach((file) => { | ||
$(`cp -r ${file} ${buildDir}`); | ||
}); | ||
const exportsMap = { | ||
...buildExportsMap(srcDirName, {}, srcDirName, filterExportsPathFn || defaultFilterExportsPathFunction), | ||
'./package.json': './package.json', | ||
}; | ||
const rootExport = exportsMap['.']; | ||
const patchedPackageJson = { | ||
...packageJson, | ||
exports: exportsMap, | ||
files: ['*'], | ||
}; | ||
if (rootExport) { | ||
if (typeof rootExport === 'string') { | ||
patchedPackageJson.main = rootExport; | ||
} | ||
else { | ||
patchedPackageJson.main = rootExport.import; | ||
patchedPackageJson.typings = rootExport.types; | ||
} | ||
} | ||
patchPackageJson?.(patchedPackageJson); | ||
writeFile(`${buildDir}/package.json`, JSON.stringify(patchedPackageJson, null, 2)); | ||
const gitDiffCachedResult = $(`git diff --cached ${rootDir}/package.json | cat`, 'pipe'); | ||
const gitDiffResult = $(`git diff ${rootDir}/package.json | cat`, 'pipe'); | ||
const versionsDiff = tryToFindPackageVersionChanged(gitDiffCachedResult) ?? | ||
tryToFindPackageVersionChanged(gitDiffResult); | ||
if (versionsDiff) { | ||
onPackageVersionChanged?.(versionsDiff.nextVersion, versionsDiff.prevVersion, utils); | ||
} | ||
}; | ||
export * from './post-build-script.js'; | ||
export * from './publish-script.js'; |
{ | ||
"name": "js2me-exports-post-build-script", | ||
"version": "2.0.4", | ||
"version": "2.0.5", | ||
"description": "", | ||
@@ -28,2 +28,12 @@ "main": "./index.js", | ||
}, | ||
"./post-build-script": { | ||
"import": "./post-build-script.js", | ||
"default": "./post-build-script.js", | ||
"types": "./post-build-script.d.ts" | ||
}, | ||
"./publish-script": { | ||
"import": "./publish-script.js", | ||
"default": "./publish-script.js", | ||
"types": "./publish-script.d.ts" | ||
}, | ||
"./types": { | ||
@@ -34,6 +44,16 @@ "import": "./types.js", | ||
}, | ||
"./utils/fs": { | ||
"import": "./utils/fs.js", | ||
"default": "./utils/fs.js", | ||
"types": "./utils/fs.d.ts" | ||
}, | ||
"./utils/get-package-version-diff": { | ||
"import": "./utils/get-package-version-diff.js", | ||
"default": "./utils/get-package-version-diff.js", | ||
"types": "./utils/get-package-version-diff.d.ts" | ||
}, | ||
"./utils": { | ||
"import": "./utils.js", | ||
"default": "./utils.js", | ||
"types": "./utils.d.ts" | ||
"import": "./utils/index.js", | ||
"default": "./utils/index.js", | ||
"types": "./utils/index.d.ts" | ||
}, | ||
@@ -40,0 +60,0 @@ "./package.json": "./package.json" |
@@ -1,3 +0,3 @@ | ||
import * as utils from './utils.js'; | ||
type UtilsType = typeof utils; | ||
import * as fsUtils from './utils/fs.js'; | ||
export type FsUtils = typeof fsUtils; | ||
export type FilterExportsPathFunction = (path: string, targetPath: string, extension: string) => boolean; | ||
@@ -32,5 +32,12 @@ export interface PostBuildScriptConfig { | ||
patchPackageJson?: (packageJson: Record<string, any>) => void; | ||
onPackageVersionChanged?: (currentVersion: string, previousVersion: string | null, utils: UtilsType) => void; | ||
onPackageVersionChanged?: (currentVersion: string, previousVersion: string | null, utils: FsUtils) => void; | ||
} | ||
export {}; | ||
export interface PublishScriptConfig { | ||
nextVersion: string; | ||
prevVersion: string | null; | ||
publishCommand: string; | ||
commitAllCurrentChanges?: boolean; | ||
createTag?: boolean; | ||
cleanupCommand?: string; | ||
} | ||
//# sourceMappingURL=types.d.ts.map |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
15384
29.09%24
100%230
22.99%1
Infinity%