Skip to content

Commit

Permalink
draft of JS API for import
Browse files Browse the repository at this point in the history
  • Loading branch information
isuvorov committed Nov 27, 2023
1 parent 0767f5e commit 16477fc
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 99 deletions.
47 changes: 2 additions & 45 deletions clean-publish.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
#!/usr/bin/env node

import { parseListArg } from './utils.js'
import {
createTempDirectory,
createFilesFilter,
copyFiles,
readPackageJSON,
clearPackageJSON,
writePackageJSON,
publish,
cleanComments,
removeTempDirectory,
runScript,
cleanDocs
} from './core.js'
import { getConfig } from './get-config.js'
import { cleanPublish } from './core.js'

const HELP =
'npx clean-publish\n' +
Expand Down Expand Up @@ -99,38 +87,7 @@ async function handleOptions() {

async function run() {
const options = await handleOptions()

const tempDirectoryName = await createTempDirectory(options.tempDir)

const filesFilter = createFilesFilter(options.files)

await copyFiles(tempDirectoryName, filesFilter)

const packageJson = await readPackageJSON()

if (options.cleanDocs) {
await cleanDocs(tempDirectoryName, packageJson.repository, packageJson.homepage)
}

if (options.cleanComments) {
await cleanComments(tempDirectoryName)
}

const cleanPackageJSON = clearPackageJSON(packageJson, options.fields)
await writePackageJSON(tempDirectoryName, cleanPackageJSON)

let prepublishSuccess = true
if (options.beforeScript) {
prepublishSuccess = await runScript(options.beforeScript, tempDirectoryName)
}

if (!options.withoutPublish && prepublishSuccess) {
await publish(tempDirectoryName, options)
}

if (!options.withoutPublish) {
await removeTempDirectory(tempDirectoryName)
}
await cleanPublish(options)
}

run().catch(error => {
Expand Down
38 changes: 38 additions & 0 deletions core.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,41 @@ export async function cleanComments(drectoryName) {
})
)
}

export async function cleanPublish(options) {
const tempDirectoryName = await createTempDirectory(options.tempDir)

const filesFilter = createFilesFilter(options.files)

await copyFiles(tempDirectoryName, filesFilter)

const packageJson = await readPackageJSON()

if (options.cleanDocs) {
await cleanDocs(
tempDirectoryName,
packageJson.repository,
packageJson.homepage
)
}

if (options.cleanComments) {
await cleanComments(tempDirectoryName)
}

const cleanPackageJSON = clearPackageJSON(packageJson, options.fields)
await writePackageJSON(tempDirectoryName, cleanPackageJSON)

let prepublishSuccess = true
if (options.beforeScript) {
prepublishSuccess = await runScript(options.beforeScript, tempDirectoryName)
}

if (!options.withoutPublish && prepublishSuccess) {
await publish(tempDirectoryName, options)
}

if (!options.withoutPublish) {
await removeTempDirectory(tempDirectoryName)
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"clean-publish": "clean-publish.js",
"clear-package-json": "clear-package-json.js"
},
"main": "./core.js",
"scripts": {
"lint": "eslint *.js test/*.js",
"unit": "tsm node_modules/uvu/bin.js . 'test/[^/]+\\.test\\.js$'",
Expand Down
120 changes: 66 additions & 54 deletions types.d.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,81 @@
export type PackageAccess = 'public' | 'restricted' | string;
export type PackageAccess = 'public' | 'restricted' | string

export type PackageManager = 'npm' | 'pnpm' | 'yarn' | string;
export type PackageManager = 'npm' | 'pnpm' | 'yarn' | string

export interface Config {
/**
* Keep only main section of `README.md`
* @default false
*/
cleanDocs?: boolean;
/**
* Keep only main section of `README.md`
* @default false
*/
cleanDocs?: boolean

/**
* Clean inline comments from JS files.
* @default false
*/
cleanComments?: boolean;
/**
* Clean inline comments from JS files.
* @default false
*/
cleanComments?: boolean

/**
* List of files that you want to delete before publishing (supports regex and glob patterns).
*/
files?: Array<string | RegExp>;
/**
* List of files that you want to delete before publishing (supports regex and glob patterns).
*/
files?: Array<string | RegExp>

/**
* List of fields in the `package.json` file that you want to delete before publishing.
*/
fields?: string[];
/**
* List of fields in the `package.json` file that you want to delete before publishing.
*/
fields?: string[]

/**
* Clean project without `npm publish` (tmp directory will not be deleted automatically).
* @default false
*/
withoutPublish?: boolean;
/**
* Clean project without `npm publish` (tmp directory will not be deleted automatically).
* @default false
*/
withoutPublish?: boolean

/**
* Name of package manager to use.
* @default "npm"
*/
packageManager?: PackageManager;
/**
* Name of package manager to use.
* @default "npm"
*/
packageManager?: PackageManager

/**
* Whether the npm registry publishes this package as a public package, or restricted.
*/
access?: PackageAccess;
/**
* Whether the npm registry publishes this package as a public package, or restricted.
*/
access?: PackageAccess

/**
* Create temporary directory with given name.
*/
tempDir?: string;
/**
* Create temporary directory with given name.
*/
tempDir?: string

/**
* Options which are directly passed into package manager during publish.
*/
packageManagerOptions?: string[];
/**
* Options which are directly passed into package manager during publish.
*/
packageManagerOptions?: string[]

/**
* Reports the details of what would have been published.
*/
dryRun?: boolean;
/**
* Reports the details of what would have been published.
*/
dryRun?: boolean

/**
* Registers the package with the given tag.
*/
tag?: string;
/**
* Registers the package with the given tag.
*/
tag?: string

/**
* Run script on the to-release dir before npm publish.
*/
beforeScript?: string;
/**
* Run script on the to-release dir before npm publish.
*/
beforeScript?: string
}

export interface CleanPublishOptions {
tempDir?: string
files?: string[]
cleanDocs?: boolean
cleanComments?: boolean
fields?: string[]
beforeScript?: string
withoutPublish?: boolean
}

declare function cleanPublish(options?: CleanPublishOptions): Promise<void>

0 comments on commit 16477fc

Please sign in to comment.