Repository: ioBroker/create-adapter
Pull request #124 (2019-03-13 14:24:44)

Add a CLI option to skip the check for adapter existence
**PR Checklist:**  
- [x] Provide a meaningful description to this PR or mention which issues this fixes.
- [x] Add tests for your change. This includes negative tests (i.e. inputs that need to fail) as well as baseline tests (i.e. how should the directory structure look like?).
- [x] Run the test suite with `npm test`
- [ ] ~~If there are baseline changes, review them and make a separate commit for them with the comment "accept baselines" if they are desired changes~~
- [x] Ensure the project builds with `npm run build`
- [ ] If you added a required option, also add it to the template creation (`travis/create_templates.ts`)
- [x] Add your changes to `CHANGELOG.md` (referencing this PR or the issue you fixed)

**Description:**  
Fixes: #122 


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 035cc83..9c620df 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,7 @@
 
 ## __WORK IN PROGRESS__
 * (AlCalzone) Enforce `js-controller >= 1.4.2` during installation
+* (AlCalzone) Added the `--skipAdapterExistenceCheck` CLI option to skip checking if an adapter already exists
 
 ## v1.10.0 (2019-02-25)
 * (AlCalzone) Add more unit tests
diff --git a/README.md b/README.md
index 2371ed4..cfb9fad 100644
--- a/README.md
+++ b/README.md
@@ -22,12 +22,16 @@ Any computer with NodeJS in version 8 or higher and npm 6 or higher.
 ## Usage
 This tool is not supposed to be installed. Instead, run the most recent version using
 ```
-npx @iobroker/create-adapter
+npx @iobroker/create-adapter [options]
 ```
 in the directory where the directory of your project should be created. You don't need to create the adapter/widget directory, because it will be created for you.
 
 After a short while, you will be asked a few questions. Afterwards all the necessary files will be created for you.
 
+### Options
+The following CLI options are available:
+* `--target=/path/to/dir` - Specify which directory the adapter files should be created in (instead of the current dir)
+* `--skipAdapterExistenceCheck` - Don't check if an adapter with the same name already exists on `npm`.
 
 ## Features
 * Choose between: ioBroker adapter, VIS widget or both
diff --git a/src/cli.ts b/src/cli.ts
index dd539f5..0054c26 100644
--- a/src/cli.ts
+++ b/src/cli.ts
@@ -10,6 +10,10 @@ import { error, executeCommand, isWindows } from "./lib/tools";
 /** Where the output should be written */
 const rootDir = path.resolve(yargs.argv.target || process.cwd());
 
+const creatorOptions = {
+	skipAdapterExistenceCheck: !!yargs.argv.skipAdapterExistenceCheck,
+};
+
 /** Asks a series of questions on the CLI */
 async function ask() {
 	let answers: Record<string, any> = { cli: true };
@@ -35,7 +39,7 @@ async function ask() {
 				}
 				// Test the result
 				if (q.action != undefined) {
-					const testResult = await q.action(answer[q.name as string]);
+					const testResult = await q.action(answer[q.name as string], creatorOptions);
 					if (typeof testResult === "string") {
 						error(testResult);
 						continue;
diff --git a/src/lib/actionsAndTransformers.ts b/src/lib/actionsAndTransformers.ts
index 227f085..5414124 100644
--- a/src/lib/actionsAndTransformers.ts
+++ b/src/lib/actionsAndTransformers.ts
@@ -35,12 +35,14 @@ async function checkAdapterExistence(name: string): Promise<CheckResult> {
 	}
 }
 
-export async function checkAdapterName(name: string): Promise<CheckResult> {
+export async function checkAdapterName<T extends { skipAdapterExistenceCheck: boolean }>(name: string, options?: T): Promise<CheckResult> {
 	const validCheck = isAdapterNameValid(name);
 	if (typeof validCheck === "string") return validCheck;
 
-	const existenceCheck = await checkAdapterExistence(name);
-	if (typeof existenceCheck === "string") return existenceCheck;
+	if (!options || !options.skipAdapterExistenceCheck) {
+		const existenceCheck = await checkAdapterExistence(name);
+		if (typeof existenceCheck === "string") return existenceCheck;
+	}
 
 	return true;
 }
@@ -93,7 +95,7 @@ export function transformKeywords(keywords: string): string[] | undefined {
 		.split(",")
 		.map(k => k.trim())
 		.filter(k => !!k)
-	;
+		;
 	if (keywordsArray.length === 0) return undefined;
 	return keywordsArray;
 }
diff --git a/src/lib/questions.ts b/src/lib/questions.ts
index daa2bf3..2f0f534 100644
--- a/src/lib/questions.ts
+++ b/src/lib/questions.ts
@@ -9,7 +9,7 @@ import { getOwnVersion } from "./tools";
 // Sadly, Enquirer does not export the PromptOptions type
 // tslint:disable-next-line:ban-types
 type PromptOptions = Exclude<Parameters<typeof prompt>[0], Function | any[]>;
-type QuestionAction<T> = (value: T) => CheckResult | Promise<CheckResult>;
+type QuestionAction<T> = (value: T, options?: unknown) => CheckResult | Promise<CheckResult>;
 // tslint:disable-next-line:interface-over-type-literal
 export type AnswerValue = string | boolean | number;
 export type Condition = { name: string } & (


Test patch:
diff --git a/src/lib/actionsAndTransformers.test.ts b/src/lib/actionsAndTransformers.test.ts
index 420c394..f7b2ce7 100644
--- a/src/lib/actionsAndTransformers.test.ts
+++ b/src/lib/actionsAndTransformers.test.ts
@@ -83,6 +83,11 @@ describe("actionsAndTransformers/checkAdapterName()", () => {
 		result.should.be.a("string").and.match(/already exists/);
 	});
 
+	it("should not return an error if the adapter exists and the check is skipped", async () => {
+		fetchPackageVersion.resolves("1.2.3");
+		await checkAdapterName("foo", {skipAdapterExistenceCheck: true}).should.become(true);
+	});
+
 	it("should return true otherwise", async () => {
 		fetchPackageVersion.rejects("404");
 		await checkAdapterName("foo").should.become(true);