@cedarjs/cli-helpers 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +3 -0
- package/dist/auth/authFiles.d.ts +44 -0
- package/dist/auth/authFiles.d.ts.map +1 -0
- package/dist/auth/authFiles.js +74 -0
- package/dist/auth/authTasks.d.ts +49 -0
- package/dist/auth/authTasks.d.ts.map +1 -0
- package/dist/auth/authTasks.js +345 -0
- package/dist/auth/setupHelpers.d.ts +26 -0
- package/dist/auth/setupHelpers.d.ts.map +1 -0
- package/dist/auth/setupHelpers.js +127 -0
- package/dist/build.d.ts +2 -0
- package/dist/build.d.ts.map +1 -0
- package/dist/cjs/auth/authFiles.d.ts +44 -0
- package/dist/cjs/auth/authFiles.d.ts.map +1 -0
- package/dist/cjs/auth/authFiles.js +109 -0
- package/dist/cjs/auth/authTasks.d.ts +49 -0
- package/dist/cjs/auth/authTasks.d.ts.map +1 -0
- package/dist/cjs/auth/authTasks.js +384 -0
- package/dist/cjs/auth/setupHelpers.d.ts +26 -0
- package/dist/cjs/auth/setupHelpers.d.ts.map +1 -0
- package/dist/cjs/auth/setupHelpers.js +151 -0
- package/dist/cjs/index.d.ts +12 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js +52 -0
- package/dist/cjs/lib/colors.d.ts +23 -0
- package/dist/cjs/lib/colors.d.ts.map +1 -0
- package/dist/cjs/lib/colors.js +52 -0
- package/dist/cjs/lib/index.d.ts +21 -0
- package/dist/cjs/lib/index.d.ts.map +1 -0
- package/dist/cjs/lib/index.js +140 -0
- package/dist/cjs/lib/installHelpers.d.ts +17 -0
- package/dist/cjs/lib/installHelpers.d.ts.map +1 -0
- package/dist/cjs/lib/installHelpers.js +72 -0
- package/dist/cjs/lib/loadEnvFiles.d.ts +5 -0
- package/dist/cjs/lib/loadEnvFiles.d.ts.map +1 -0
- package/dist/cjs/lib/loadEnvFiles.js +101 -0
- package/dist/cjs/lib/paths.d.ts +6 -0
- package/dist/cjs/lib/paths.d.ts.map +1 -0
- package/dist/cjs/lib/paths.js +42 -0
- package/dist/cjs/lib/project.d.ts +42 -0
- package/dist/cjs/lib/project.d.ts.map +1 -0
- package/dist/cjs/lib/project.js +265 -0
- package/dist/cjs/lib/version.d.ts +17 -0
- package/dist/cjs/lib/version.d.ts.map +1 -0
- package/dist/cjs/lib/version.js +107 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/telemetry/index.d.ts +20 -0
- package/dist/cjs/telemetry/index.d.ts.map +1 -0
- package/dist/cjs/telemetry/index.js +62 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +20 -0
- package/dist/lib/colors.d.ts +23 -0
- package/dist/lib/colors.d.ts.map +1 -0
- package/dist/lib/colors.js +18 -0
- package/dist/lib/index.d.ts +21 -0
- package/dist/lib/index.d.ts.map +1 -0
- package/dist/lib/index.js +102 -0
- package/dist/lib/installHelpers.d.ts +17 -0
- package/dist/lib/installHelpers.d.ts.map +1 -0
- package/dist/lib/installHelpers.js +35 -0
- package/dist/lib/loadEnvFiles.d.ts +5 -0
- package/dist/lib/loadEnvFiles.d.ts.map +1 -0
- package/dist/lib/loadEnvFiles.js +64 -0
- package/dist/lib/paths.d.ts +6 -0
- package/dist/lib/paths.d.ts.map +1 -0
- package/dist/lib/paths.js +18 -0
- package/dist/lib/project.d.ts +42 -0
- package/dist/lib/project.d.ts.map +1 -0
- package/dist/lib/project.js +227 -0
- package/dist/lib/version.d.ts +17 -0
- package/dist/lib/version.d.ts.map +1 -0
- package/dist/lib/version.js +73 -0
- package/dist/package.json +1 -0
- package/dist/src/auth/__tests__/authFiles.test.d.ts +2 -0
- package/dist/src/auth/__tests__/authFiles.test.d.ts.map +1 -0
- package/dist/src/auth/__tests__/authTasks.test.d.ts +2 -0
- package/dist/src/auth/__tests__/authTasks.test.d.ts.map +1 -0
- package/dist/src/auth/__tests__/mockFsFiles.d.ts +14 -0
- package/dist/src/auth/__tests__/mockFsFiles.d.ts.map +1 -0
- package/dist/src/auth/__tests__/setupHelpers.test.d.ts +2 -0
- package/dist/src/auth/__tests__/setupHelpers.test.d.ts.map +1 -0
- package/dist/src/auth/authFiles.d.ts +44 -0
- package/dist/src/auth/authFiles.d.ts.map +1 -0
- package/dist/src/auth/authTasks.d.ts +49 -0
- package/dist/src/auth/authTasks.d.ts.map +1 -0
- package/dist/src/auth/setupHelpers.d.ts +26 -0
- package/dist/src/auth/setupHelpers.d.ts.map +1 -0
- package/dist/src/index.d.ts +12 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/lib/__tests__/index.test.d.ts +2 -0
- package/dist/src/lib/__tests__/index.test.d.ts.map +1 -0
- package/dist/src/lib/__tests__/loadEnvFiles.test.d.ts +2 -0
- package/dist/src/lib/__tests__/loadEnvFiles.test.d.ts.map +1 -0
- package/dist/src/lib/__tests__/project.addTomlSetting.test.d.ts +2 -0
- package/dist/src/lib/__tests__/project.addTomlSetting.test.d.ts.map +1 -0
- package/dist/src/lib/__tests__/project.test.d.ts +2 -0
- package/dist/src/lib/__tests__/project.test.d.ts.map +1 -0
- package/dist/src/lib/__tests__/version.test.d.ts +2 -0
- package/dist/src/lib/__tests__/version.test.d.ts.map +1 -0
- package/dist/src/lib/colors.d.ts +23 -0
- package/dist/src/lib/colors.d.ts.map +1 -0
- package/dist/src/lib/index.d.ts +21 -0
- package/dist/src/lib/index.d.ts.map +1 -0
- package/dist/src/lib/installHelpers.d.ts +17 -0
- package/dist/src/lib/installHelpers.d.ts.map +1 -0
- package/dist/src/lib/loadEnvFiles.d.ts +5 -0
- package/dist/src/lib/loadEnvFiles.d.ts.map +1 -0
- package/dist/src/lib/paths.d.ts +6 -0
- package/dist/src/lib/paths.d.ts.map +1 -0
- package/dist/src/lib/project.d.ts +42 -0
- package/dist/src/lib/project.d.ts.map +1 -0
- package/dist/src/lib/version.d.ts +17 -0
- package/dist/src/lib/version.d.ts.map +1 -0
- package/dist/src/telemetry/index.d.ts +20 -0
- package/dist/src/telemetry/index.d.ts.map +1 -0
- package/dist/telemetry/index.d.ts +20 -0
- package/dist/telemetry/index.d.ts.map +1 -0
- package/dist/telemetry/index.js +27 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/vitest.config.d.mts +3 -0
- package/dist/vitest.config.d.mts.map +1 -0
- package/package.json +91 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Cedar
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
interface FilesArgs {
|
|
2
|
+
basedir: string;
|
|
3
|
+
webAuthn: boolean;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Get the api side file paths and file contents to write
|
|
7
|
+
*
|
|
8
|
+
* Example return value:
|
|
9
|
+
* ```json
|
|
10
|
+
* {
|
|
11
|
+
* "/Users/tobbe/dev/rw-app/api/src/lib/auth.ts": "<file content>",
|
|
12
|
+
* "/Users/tobbe/dev/rw-app/api/src/lib/helperFunctions.ts": "<file content>",
|
|
13
|
+
* "/Users/tobbe/dev/rw-app/api/src/functions/auth.ts": "<file content>"
|
|
14
|
+
* }
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export declare const apiSideFiles: ({ basedir, webAuthn }: FilesArgs) => Promise<Record<string, string>>;
|
|
18
|
+
/**
|
|
19
|
+
* Loops through the keys in `filesRecord` and generates unique file paths if
|
|
20
|
+
* they conflict with existing files
|
|
21
|
+
*
|
|
22
|
+
* Given this input:
|
|
23
|
+
* ```json
|
|
24
|
+
* {
|
|
25
|
+
* "/Users/tobbe/dev/rw-app/api/src/lib/auth.ts": "<file content>",
|
|
26
|
+
* "/Users/tobbe/dev/rw-app/api/src/lib/helperFunctions.ts": "<file content>",
|
|
27
|
+
* "/Users/tobbe/dev/rw-app/api/src/lib/supertokens.ts": "<file content>",
|
|
28
|
+
* "/Users/tobbe/dev/rw-app/api/src/functions/auth.ts": "<file content>"
|
|
29
|
+
* }
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* You could get this output, depending on what existing files there are
|
|
33
|
+
* ```json
|
|
34
|
+
* {
|
|
35
|
+
* "/Users/tobbe/dev/rw-app/api/src/lib/supertokensAuth3.ts": "<file content>",
|
|
36
|
+
* "/Users/tobbe/dev/rw-app/api/src/lib/supertokensHelperFunctions.ts": "<file content>",
|
|
37
|
+
* "/Users/tobbe/dev/rw-app/api/src/lib/supertokens2.ts": "<file content>",
|
|
38
|
+
* "/Users/tobbe/dev/rw-app/api/src/functions/auth.ts": "<file content>"
|
|
39
|
+
* }
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export declare function generateUniqueFileNames(filesRecord: Record<string, string>, provider: string): Record<string, string>;
|
|
43
|
+
export {};
|
|
44
|
+
//# sourceMappingURL=authFiles.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"authFiles.d.ts","sourceRoot":"","sources":["../../src/auth/authFiles.ts"],"names":[],"mappings":"AASA,UAAU,SAAS;IACjB,OAAO,EAAE,MAAM,CAAA;IACf,QAAQ,EAAE,OAAO,CAAA;CAClB;AAED;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,YAAY,0BAAiC,SAAS,oCAmElE,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,uBAAuB,CACrC,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EACnC,QAAQ,EAAE,MAAM,0BAwCjB"}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import pascalcase from "pascalcase";
|
|
4
|
+
import { transformTSToJS } from "../lib/index.js";
|
|
5
|
+
import { getPaths } from "../lib/paths.js";
|
|
6
|
+
import { isTypeScriptProject } from "../lib/project.js";
|
|
7
|
+
const apiSideFiles = async ({ basedir, webAuthn }) => {
|
|
8
|
+
const apiSrcPath = getPaths().api.src;
|
|
9
|
+
const apiBaseTemplatePath = path.join(basedir, "templates", "api");
|
|
10
|
+
const templateDirectories = fs.readdirSync(apiBaseTemplatePath);
|
|
11
|
+
let filesRecord = {};
|
|
12
|
+
for (const dir of templateDirectories) {
|
|
13
|
+
const templateFiles = fs.readdirSync(path.join(apiBaseTemplatePath, dir));
|
|
14
|
+
const filePaths = templateFiles.filter((fileName) => {
|
|
15
|
+
const fileNameParts = fileName.split(".");
|
|
16
|
+
return fileNameParts.length <= 3 || fileNameParts.at(-3) !== "webAuthn";
|
|
17
|
+
}).map((fileName) => {
|
|
18
|
+
let outputFileName = fileName.replace(/\.template$/, "");
|
|
19
|
+
if (!isTypeScriptProject()) {
|
|
20
|
+
outputFileName = outputFileName.replace(/\.ts(x?)$/, ".js$1");
|
|
21
|
+
}
|
|
22
|
+
if (!webAuthn) {
|
|
23
|
+
return { templateFileName: fileName, outputFileName };
|
|
24
|
+
}
|
|
25
|
+
const webAuthnFileName = fileName.split(".").reverse().map((part, i) => i === 1 ? "webAuthn." + part : part).reverse().join(".");
|
|
26
|
+
if (templateFiles.includes(webAuthnFileName)) {
|
|
27
|
+
return { templateFileName: webAuthnFileName, outputFileName };
|
|
28
|
+
} else {
|
|
29
|
+
return { templateFileName: fileName, outputFileName };
|
|
30
|
+
}
|
|
31
|
+
}).map((f) => {
|
|
32
|
+
const templateFilePath = path.join(
|
|
33
|
+
apiBaseTemplatePath,
|
|
34
|
+
dir,
|
|
35
|
+
f.templateFileName
|
|
36
|
+
);
|
|
37
|
+
const outputFilePath = path.join(apiSrcPath, dir, f.outputFileName);
|
|
38
|
+
return { templateFilePath, outputFilePath };
|
|
39
|
+
});
|
|
40
|
+
for (const paths of filePaths) {
|
|
41
|
+
const content = fs.readFileSync(paths.templateFilePath, "utf8");
|
|
42
|
+
filesRecord = {
|
|
43
|
+
...filesRecord,
|
|
44
|
+
[paths.outputFilePath]: isTypeScriptProject() ? content : await transformTSToJS(paths.outputFilePath, content)
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return filesRecord;
|
|
49
|
+
};
|
|
50
|
+
function generateUniqueFileNames(filesRecord, provider) {
|
|
51
|
+
const newFilesRecord = {};
|
|
52
|
+
Object.keys(filesRecord).forEach((fullPath) => {
|
|
53
|
+
let newFullPath = fullPath;
|
|
54
|
+
let i = 1;
|
|
55
|
+
while (fs.existsSync(newFullPath)) {
|
|
56
|
+
const nameParts = path.basename(fullPath).split(".");
|
|
57
|
+
if (nameParts[0] === provider) {
|
|
58
|
+
const newFileName = provider + (i + 1) + "." + nameParts.slice(1).join(".");
|
|
59
|
+
newFullPath = path.join(path.dirname(fullPath), newFileName);
|
|
60
|
+
} else {
|
|
61
|
+
const count = i > 1 ? i : "";
|
|
62
|
+
const newFileName = provider + pascalcase(nameParts[0]) + count + "." + nameParts.slice(1).join(".");
|
|
63
|
+
newFullPath = path.join(path.dirname(fullPath), newFileName);
|
|
64
|
+
}
|
|
65
|
+
i++;
|
|
66
|
+
}
|
|
67
|
+
newFilesRecord[newFullPath] = filesRecord[fullPath];
|
|
68
|
+
});
|
|
69
|
+
return newFilesRecord;
|
|
70
|
+
}
|
|
71
|
+
export {
|
|
72
|
+
apiSideFiles,
|
|
73
|
+
generateUniqueFileNames
|
|
74
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { ListrRenderer, ListrTask, ListrTaskWrapper } from 'listr2';
|
|
2
|
+
export declare const getWebAppPath: () => string;
|
|
3
|
+
export declare const addApiConfig: ({ replaceExistingImport, authDecoderImport, }: {
|
|
4
|
+
replaceExistingImport: boolean;
|
|
5
|
+
authDecoderImport?: string;
|
|
6
|
+
}) => void;
|
|
7
|
+
export declare const hasAuthProvider: (content: string) => boolean;
|
|
8
|
+
/**
|
|
9
|
+
* Removes <AuthProvider ...> and </AuthProvider> if they exist, and un-indents
|
|
10
|
+
* the content.
|
|
11
|
+
*
|
|
12
|
+
* Exported for testing
|
|
13
|
+
*/
|
|
14
|
+
export declare const removeAuthProvider: (content: string) => string;
|
|
15
|
+
/**
|
|
16
|
+
* Actually inserts the required config lines into App.{jsx,tsx}
|
|
17
|
+
* Exported for testing
|
|
18
|
+
*/
|
|
19
|
+
export declare const addConfigToWebApp: <Renderer extends typeof ListrRenderer>() => ListrTask<AuthGeneratorCtx, Renderer>;
|
|
20
|
+
export declare const createWebAuth: (basedir: string, webAuthn: boolean) => {
|
|
21
|
+
title: string;
|
|
22
|
+
task: (ctx: AuthGeneratorCtx) => Promise<void>;
|
|
23
|
+
};
|
|
24
|
+
export declare const addConfigToRoutes: () => {
|
|
25
|
+
title: string;
|
|
26
|
+
task: () => void;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Will find the templates inside `${basedir}/templates/api`,
|
|
30
|
+
* and write these files to disk with unique names if they are clashing.
|
|
31
|
+
*
|
|
32
|
+
* @returns Listr task
|
|
33
|
+
*/
|
|
34
|
+
export declare const generateAuthApiFiles: <Renderer extends typeof ListrRenderer>(basedir: string, webAuthn: boolean) => ListrTask<AuthGeneratorCtx, Renderer>;
|
|
35
|
+
export declare const addAuthConfigToGqlApi: <Renderer extends typeof ListrRenderer, FallbackRenderer extends typeof ListrRenderer>(authDecoderImport?: string) => {
|
|
36
|
+
title: string;
|
|
37
|
+
task: (ctx: AuthGeneratorCtx, _task: ListrTaskWrapper<AuthGeneratorCtx, Renderer, FallbackRenderer>) => void;
|
|
38
|
+
};
|
|
39
|
+
export type AuthSetupMode = 'FORCE' | 'REPLACE' | 'COMBINE' | 'UNKNOWN';
|
|
40
|
+
export interface AuthGeneratorCtx {
|
|
41
|
+
setupMode: AuthSetupMode;
|
|
42
|
+
provider: string;
|
|
43
|
+
force: boolean;
|
|
44
|
+
}
|
|
45
|
+
export declare const setAuthSetupMode: <Renderer extends typeof ListrRenderer, FallbackRenderer extends typeof ListrRenderer>(force: boolean) => {
|
|
46
|
+
title: string;
|
|
47
|
+
task: (ctx: AuthGeneratorCtx, task: ListrTaskWrapper<AuthGeneratorCtx, Renderer, FallbackRenderer>) => Promise<void>;
|
|
48
|
+
};
|
|
49
|
+
//# sourceMappingURL=authTasks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"authTasks.d.ts","sourceRoot":"","sources":["../../src/auth/authTasks.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,aAAa,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,QAAQ,CAAA;AAmBxE,eAAO,MAAM,aAAa,cAA2B,CAAA;AAwDrD,eAAO,MAAM,YAAY,kDAGtB;IACD,qBAAqB,EAAE,OAAO,CAAA;IAC9B,iBAAiB,CAAC,EAAE,MAAM,CAAA;CAC3B,SAgDA,CAAA;AAmCD,eAAO,MAAM,eAAe,YAAa,MAAM,YAE9C,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,kBAAkB,YAAa,MAAM,WA6CjD,CAAA;AAoED;;;GAGG;AACH,eAAO,MAAM,iBAAiB,GAC5B,QAAQ,SAAS,OAAO,aAAa,OAClC,SAAS,CAAC,gBAAgB,EAAE,QAAQ,CA0CxC,CAAA;AAED,eAAO,MAAM,aAAa,YAAa,MAAM,YAAY,OAAO;;gBAgC1C,gBAAgB;CA0CrC,CAAA;AAED,eAAO,MAAM,iBAAiB;;;CAmB7B,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,oBAAoB,GAAI,QAAQ,SAAS,OAAO,aAAa,WAC/D,MAAM,YACL,OAAO,KAChB,SAAS,CAAC,gBAAgB,EAAE,QAAQ,CAgDtC,CAAA;AASD,eAAO,MAAM,qBAAqB,GAChC,QAAQ,SAAS,OAAO,aAAa,EACrC,gBAAgB,SAAS,OAAO,aAAa,sBAEzB,MAAM;;gBAInB,gBAAgB,SACd,gBAAgB,CAAC,gBAAgB,EAAE,QAAQ,EAAE,gBAAgB,CAAC;CAcvE,CAAA;AAEF,MAAM,MAAM,aAAa,GACrB,OAAO,GACP,SAAS,GACT,SAAS,GACT,SAAS,CAAA;AAEb,MAAM,WAAW,gBAAgB;IAC/B,SAAS,EAAE,aAAa,CAAA;IACxB,QAAQ,EAAE,MAAM,CAAA;IAChB,KAAK,EAAE,OAAO,CAAA;CACf;AAED,eAAO,MAAM,gBAAgB,GAC3B,QAAQ,SAAS,OAAO,aAAa,EACrC,gBAAgB,SAAS,OAAO,aAAa,SAEtC,OAAO;;gBAKL,gBAAgB,QACf,gBAAgB,CAAC,gBAAgB,EAAE,QAAQ,EAAE,gBAAgB,CAAC;CA2CzE,CAAA"}
|
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { ListrEnquirerPromptAdapter } from "@listr2/prompt-adapter-enquirer";
|
|
4
|
+
import { getConfig, resolveFile } from "@cedarjs/project-config";
|
|
5
|
+
import { colors } from "../lib/colors.js";
|
|
6
|
+
import { transformTSToJS, writeFilesTask } from "../lib/index.js";
|
|
7
|
+
import { getPaths } from "../lib/paths.js";
|
|
8
|
+
import {
|
|
9
|
+
getGraphqlPath,
|
|
10
|
+
graphFunctionDoesExist,
|
|
11
|
+
isTypeScriptProject
|
|
12
|
+
} from "../lib/project.js";
|
|
13
|
+
import { apiSideFiles, generateUniqueFileNames } from "./authFiles.js";
|
|
14
|
+
const AUTH_PROVIDER_HOOK_IMPORT = `import { AuthProvider, useAuth } from './auth'`;
|
|
15
|
+
const AUTH_HOOK_IMPORT = `import { useAuth } from './auth'`;
|
|
16
|
+
const getWebAppPath = () => getPaths().web.app;
|
|
17
|
+
function addAuthDecoderToCreateGraphQLHandler(content) {
|
|
18
|
+
if (!new RegExp("(?=(^.*?createGraphQLHandler))\\1.*\\bauthDecoder", "s").test(
|
|
19
|
+
content
|
|
20
|
+
)) {
|
|
21
|
+
return content.replace(
|
|
22
|
+
/^(?<indentation>\s*)(loggerConfig:)(.*)$/m,
|
|
23
|
+
`$<indentation>authDecoder,
|
|
24
|
+
$<indentation>$2$3`
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
return content;
|
|
28
|
+
}
|
|
29
|
+
function replaceAuthDecoderImport(content, decoderImport) {
|
|
30
|
+
return content.replace(/^import { authDecoder .*} from .+/, decoderImport);
|
|
31
|
+
}
|
|
32
|
+
function replaceAuthDecoderArg(content) {
|
|
33
|
+
return content.replace(/^(\s+)authDecoder\b.+/m, "$1authDecoder,");
|
|
34
|
+
}
|
|
35
|
+
const addApiConfig = ({
|
|
36
|
+
replaceExistingImport,
|
|
37
|
+
authDecoderImport
|
|
38
|
+
}) => {
|
|
39
|
+
const graphqlPath = getGraphqlPath();
|
|
40
|
+
if (!graphqlPath) {
|
|
41
|
+
throw new Error("Could not find your graphql file path");
|
|
42
|
+
}
|
|
43
|
+
const content = fs.readFileSync(graphqlPath).toString();
|
|
44
|
+
let newContent = content;
|
|
45
|
+
if (authDecoderImport) {
|
|
46
|
+
if (replaceExistingImport) {
|
|
47
|
+
newContent = replaceAuthDecoderImport(newContent, authDecoderImport);
|
|
48
|
+
newContent = replaceAuthDecoderArg(newContent);
|
|
49
|
+
}
|
|
50
|
+
const didReplace = newContent.includes(authDecoderImport);
|
|
51
|
+
if (!replaceExistingImport || !didReplace) {
|
|
52
|
+
newContent = authDecoderImport + "\n" + newContent;
|
|
53
|
+
newContent = addAuthDecoderToCreateGraphQLHandler(newContent);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
const hasCurrentUserImport = /(^import {.*?getCurrentUser(?!getCurrentUser).*?} from ['"]src\/lib\/auth['"])/s.test(
|
|
57
|
+
newContent
|
|
58
|
+
);
|
|
59
|
+
if (!hasCurrentUserImport) {
|
|
60
|
+
newContent = newContent.replace(
|
|
61
|
+
/^(import { db } from ['"]src\/lib\/db['"])$/m,
|
|
62
|
+
`import { getCurrentUser } from 'src/lib/auth'
|
|
63
|
+
$1`
|
|
64
|
+
);
|
|
65
|
+
newContent = newContent.replace(
|
|
66
|
+
/^(\s*)(loggerConfig:)(.*)$/m,
|
|
67
|
+
`$1getCurrentUser,
|
|
68
|
+
$1$2$3`
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
if (newContent !== content) {
|
|
72
|
+
fs.writeFileSync(graphqlPath, newContent);
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
const apiSrcDoesExist = () => {
|
|
76
|
+
return fs.existsSync(path.join(getPaths().api.src));
|
|
77
|
+
};
|
|
78
|
+
const addAuthImportToApp = (content) => {
|
|
79
|
+
const contentLines = content.split("\n");
|
|
80
|
+
const importIndex = contentLines.findLastIndex(
|
|
81
|
+
(line) => /^\s*import (?!.*(?:.css['"]|.scss['"]))/.test(line)
|
|
82
|
+
);
|
|
83
|
+
contentLines.splice(importIndex + 1, 0, "", AUTH_PROVIDER_HOOK_IMPORT);
|
|
84
|
+
return contentLines.join("\n");
|
|
85
|
+
};
|
|
86
|
+
const addAuthImportToRoutes = (content) => {
|
|
87
|
+
const contentLines = content.split("\n");
|
|
88
|
+
const importIndex = contentLines.findLastIndex(
|
|
89
|
+
(line) => /^\s*import (?!.*(?:.css['"]|.scss['"]))/.test(line)
|
|
90
|
+
);
|
|
91
|
+
contentLines.splice(importIndex + 1, 0, "", AUTH_HOOK_IMPORT);
|
|
92
|
+
return contentLines.join("\n");
|
|
93
|
+
};
|
|
94
|
+
const hasAuthProvider = (content) => {
|
|
95
|
+
return /\s*<AuthProvider([\s>]|$)/.test(content);
|
|
96
|
+
};
|
|
97
|
+
const removeAuthProvider = (content) => {
|
|
98
|
+
let remove = false;
|
|
99
|
+
let end = "";
|
|
100
|
+
let unindent = false;
|
|
101
|
+
return content.split("\n").reduce((acc, line) => {
|
|
102
|
+
let keep = !remove;
|
|
103
|
+
if (hasAuthProvider(line)) {
|
|
104
|
+
remove = true;
|
|
105
|
+
keep = false;
|
|
106
|
+
unindent = true;
|
|
107
|
+
end = line.replace(/^(\s*)<Auth.*/s, "$1") + ">";
|
|
108
|
+
}
|
|
109
|
+
if (hasAuthProvider(line) && line.trimEnd().at(-1) === ">" || line.trimEnd() === end) {
|
|
110
|
+
remove = false;
|
|
111
|
+
}
|
|
112
|
+
if (/\s*<\/AuthProvider>/.test(line)) {
|
|
113
|
+
keep = false;
|
|
114
|
+
unindent = false;
|
|
115
|
+
}
|
|
116
|
+
if (keep) {
|
|
117
|
+
if (unindent) {
|
|
118
|
+
acc.push(line.replace(" ", ""));
|
|
119
|
+
} else {
|
|
120
|
+
acc.push(line);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return acc;
|
|
124
|
+
}, []).join("\n");
|
|
125
|
+
};
|
|
126
|
+
const addAuthProviderToApp = (content, setupMode) => {
|
|
127
|
+
if (setupMode === "FORCE" || setupMode === "REPLACE") {
|
|
128
|
+
content = removeAuthProvider(content);
|
|
129
|
+
}
|
|
130
|
+
const match = content.match(
|
|
131
|
+
/(\s+)(<RedwoodProvider.*?>)(.*)(<\/RedwoodProvider>)/s
|
|
132
|
+
);
|
|
133
|
+
if (!match) {
|
|
134
|
+
throw new Error("Could not find <RedwoodProvider> in App.{jsx,tsx}");
|
|
135
|
+
}
|
|
136
|
+
if (/\s+<AuthProvider>/.test(content)) {
|
|
137
|
+
return content;
|
|
138
|
+
}
|
|
139
|
+
const [
|
|
140
|
+
_,
|
|
141
|
+
newlineAndIndent,
|
|
142
|
+
redwoodProviderOpen,
|
|
143
|
+
redwoodProviderChildren,
|
|
144
|
+
redwoodProviderClose
|
|
145
|
+
] = match;
|
|
146
|
+
const redwoodProviderChildrenLines = redwoodProviderChildren.split("\n").map((line, index) => {
|
|
147
|
+
return `${index === 0 ? "" : " "}` + line;
|
|
148
|
+
});
|
|
149
|
+
const renderContent = newlineAndIndent + redwoodProviderOpen + newlineAndIndent + ` <AuthProvider>` + redwoodProviderChildrenLines.join("\n") + `</AuthProvider>` + newlineAndIndent + redwoodProviderClose;
|
|
150
|
+
return content.replace(
|
|
151
|
+
/\s+<RedwoodProvider.*?>.*<\/RedwoodProvider>/s,
|
|
152
|
+
renderContent
|
|
153
|
+
);
|
|
154
|
+
};
|
|
155
|
+
const hasUseAuthHook = (componentName, content) => {
|
|
156
|
+
return new RegExp(
|
|
157
|
+
`<${componentName}.*useAuth={.*?}.*?>.*</${componentName}>`,
|
|
158
|
+
"s"
|
|
159
|
+
).test(content);
|
|
160
|
+
};
|
|
161
|
+
const addUseAuthHook = (componentName, content) => {
|
|
162
|
+
return content.replace(
|
|
163
|
+
`<${componentName}`,
|
|
164
|
+
`<${componentName} useAuth={useAuth}`
|
|
165
|
+
);
|
|
166
|
+
};
|
|
167
|
+
const addConfigToWebApp = () => {
|
|
168
|
+
return {
|
|
169
|
+
title: "Updating web/src/App.{jsx,tsx}",
|
|
170
|
+
task: (ctx, task) => {
|
|
171
|
+
const webAppPath = getWebAppPath();
|
|
172
|
+
if (!fs.existsSync(webAppPath)) {
|
|
173
|
+
const ext = isTypeScriptProject() ? "tsx" : "jsx";
|
|
174
|
+
throw new Error(`Could not find root App.${ext}`);
|
|
175
|
+
}
|
|
176
|
+
let content = fs.readFileSync(webAppPath, "utf-8");
|
|
177
|
+
if (!content.includes(AUTH_PROVIDER_HOOK_IMPORT)) {
|
|
178
|
+
content = addAuthImportToApp(content);
|
|
179
|
+
}
|
|
180
|
+
if (ctx.setupMode === "REPLACE" || ctx.setupMode === "FORCE") {
|
|
181
|
+
content = content.replace(
|
|
182
|
+
"import { AuthProvider } from '@cedarjs/auth'\n",
|
|
183
|
+
""
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
content = addAuthProviderToApp(content, ctx.setupMode);
|
|
187
|
+
if (/\s*<RedwoodApolloProvider/.test(content)) {
|
|
188
|
+
if (!hasUseAuthHook("RedwoodApolloProvider", content)) {
|
|
189
|
+
content = addUseAuthHook("RedwoodApolloProvider", content);
|
|
190
|
+
}
|
|
191
|
+
} else {
|
|
192
|
+
task.output = colors.warning(
|
|
193
|
+
"Could not find <RedwoodApolloProvider>.\nIf you are using a custom GraphQL Client you will have to make sure it gets access to your `useAuth`, if it needs it."
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
fs.writeFileSync(webAppPath, content);
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
};
|
|
200
|
+
const createWebAuth = (basedir, webAuthn) => {
|
|
201
|
+
const templatesBaseDir = path.join(basedir, "templates", "web");
|
|
202
|
+
const templates = fs.readdirSync(templatesBaseDir);
|
|
203
|
+
const rscEnabled = getConfig().experimental?.rsc?.enabled;
|
|
204
|
+
const templateStart = "auth" + (webAuthn ? ".webAuthn" : "") + (rscEnabled ? ".rsc" : "") + ".ts";
|
|
205
|
+
const templateFileName = templates.find((template) => {
|
|
206
|
+
return template.startsWith(templateStart);
|
|
207
|
+
});
|
|
208
|
+
if (!templateFileName) {
|
|
209
|
+
throw new Error(
|
|
210
|
+
"Could not find the auth.ts(x) template, looking for filename starting with " + templateStart
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
const templateExtension = templateFileName.split(".").at(-2);
|
|
214
|
+
const isTSProject = isTypeScriptProject();
|
|
215
|
+
let ext = templateExtension;
|
|
216
|
+
if (!isTypeScriptProject()) {
|
|
217
|
+
ext = ext?.replace("ts", "js");
|
|
218
|
+
}
|
|
219
|
+
return {
|
|
220
|
+
title: `Creating web/src/auth.${ext}`,
|
|
221
|
+
task: async (ctx) => {
|
|
222
|
+
let authFileName = path.join(getPaths().web.src, "auth");
|
|
223
|
+
if (ctx.setupMode === "COMBINE") {
|
|
224
|
+
let i = 1;
|
|
225
|
+
while (resolveFile(authFileName)) {
|
|
226
|
+
const count = i > 1 ? i : "";
|
|
227
|
+
authFileName = path.join(
|
|
228
|
+
getPaths().web.src,
|
|
229
|
+
ctx.provider + "Auth" + count
|
|
230
|
+
);
|
|
231
|
+
i++;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
authFileName = authFileName + "." + ext;
|
|
235
|
+
let template = fs.readFileSync(
|
|
236
|
+
path.join(templatesBaseDir, templateFileName),
|
|
237
|
+
"utf-8"
|
|
238
|
+
);
|
|
239
|
+
template = isTSProject ? template : await transformTSToJS(authFileName, template);
|
|
240
|
+
fs.writeFileSync(authFileName, template);
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
};
|
|
244
|
+
const addConfigToRoutes = () => {
|
|
245
|
+
return {
|
|
246
|
+
title: "Updating Routes file...",
|
|
247
|
+
task: () => {
|
|
248
|
+
const webRoutesPath = getPaths().web.routes;
|
|
249
|
+
let content = fs.readFileSync(webRoutesPath).toString();
|
|
250
|
+
if (!content.includes(AUTH_HOOK_IMPORT)) {
|
|
251
|
+
content = addAuthImportToRoutes(content);
|
|
252
|
+
}
|
|
253
|
+
if (!hasUseAuthHook("Router", content)) {
|
|
254
|
+
content = addUseAuthHook("Router", content);
|
|
255
|
+
}
|
|
256
|
+
fs.writeFileSync(webRoutesPath, content);
|
|
257
|
+
}
|
|
258
|
+
};
|
|
259
|
+
};
|
|
260
|
+
const generateAuthApiFiles = (basedir, webAuthn) => {
|
|
261
|
+
return {
|
|
262
|
+
title: "Generating auth api side files...",
|
|
263
|
+
task: async (ctx, task) => {
|
|
264
|
+
if (!apiSrcDoesExist()) {
|
|
265
|
+
return new Error(
|
|
266
|
+
"Could not find api/src directory. Cannot continue setup!"
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
let filesRecord = await apiSideFiles({ basedir, webAuthn });
|
|
270
|
+
let existingFiles = "FAIL";
|
|
271
|
+
if (ctx.setupMode === "FORCE") {
|
|
272
|
+
existingFiles = "OVERWRITE";
|
|
273
|
+
} else if (ctx.setupMode === "REPLACE") {
|
|
274
|
+
const filesToOverwrite = findExistingFiles(filesRecord);
|
|
275
|
+
const prompt = task.prompt(ListrEnquirerPromptAdapter);
|
|
276
|
+
const overwrite = await prompt.run({
|
|
277
|
+
type: "confirm",
|
|
278
|
+
message: `Overwrite existing ${filesToOverwrite.join(", ")}?`,
|
|
279
|
+
initial: false
|
|
280
|
+
});
|
|
281
|
+
existingFiles = overwrite ? "OVERWRITE" : "SKIP";
|
|
282
|
+
} else if (ctx.setupMode === "COMBINE") {
|
|
283
|
+
const uniqueFilesRecord = generateUniqueFileNames(
|
|
284
|
+
filesRecord,
|
|
285
|
+
ctx.provider
|
|
286
|
+
);
|
|
287
|
+
filesRecord = uniqueFilesRecord;
|
|
288
|
+
existingFiles = "FAIL";
|
|
289
|
+
}
|
|
290
|
+
return writeFilesTask(filesRecord, {
|
|
291
|
+
existingFiles
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
};
|
|
296
|
+
function findExistingFiles(filesMap) {
|
|
297
|
+
return Object.keys(filesMap).filter((filePath) => fs.existsSync(filePath)).map((filePath) => filePath.replace(getPaths().base, ""));
|
|
298
|
+
}
|
|
299
|
+
const addAuthConfigToGqlApi = (authDecoderImport) => ({
|
|
300
|
+
title: "Adding auth config to GraphQL API...",
|
|
301
|
+
task: (ctx, _task) => {
|
|
302
|
+
if (graphFunctionDoesExist()) {
|
|
303
|
+
addApiConfig({
|
|
304
|
+
authDecoderImport,
|
|
305
|
+
replaceExistingImport: ctx.setupMode === "REPLACE" || ctx.setupMode === "FORCE"
|
|
306
|
+
});
|
|
307
|
+
} else {
|
|
308
|
+
throw new Error(
|
|
309
|
+
"GraphQL function not found. You will need to pass the decoder to the createGraphQLHandler function."
|
|
310
|
+
);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
});
|
|
314
|
+
const setAuthSetupMode = (force) => {
|
|
315
|
+
return {
|
|
316
|
+
title: "Checking project for existing auth...",
|
|
317
|
+
task: async (ctx, task) => {
|
|
318
|
+
if (force) {
|
|
319
|
+
ctx.setupMode = "FORCE";
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
const webAppContents = fs.readFileSync(getWebAppPath(), "utf-8");
|
|
323
|
+
if (hasAuthProvider(webAppContents) && ctx.setupMode === "UNKNOWN") {
|
|
324
|
+
const setupMode = "REPLACE";
|
|
325
|
+
ctx.setupMode = setupMode;
|
|
326
|
+
return;
|
|
327
|
+
} else {
|
|
328
|
+
ctx.setupMode = "FORCE";
|
|
329
|
+
task.skip("Setting up Auth from scratch");
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
};
|
|
333
|
+
};
|
|
334
|
+
export {
|
|
335
|
+
addApiConfig,
|
|
336
|
+
addAuthConfigToGqlApi,
|
|
337
|
+
addConfigToRoutes,
|
|
338
|
+
addConfigToWebApp,
|
|
339
|
+
createWebAuth,
|
|
340
|
+
generateAuthApiFiles,
|
|
341
|
+
getWebAppPath,
|
|
342
|
+
hasAuthProvider,
|
|
343
|
+
removeAuthProvider,
|
|
344
|
+
setAuthSetupMode
|
|
345
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { ListrTask } from 'listr2';
|
|
2
|
+
import type { Argv } from 'yargs';
|
|
3
|
+
import type { AuthGeneratorCtx } from './authTasks.js';
|
|
4
|
+
export declare const standardAuthBuilder: (yargs: Argv) => Argv<{
|
|
5
|
+
force: boolean;
|
|
6
|
+
} & {
|
|
7
|
+
verbose: boolean;
|
|
8
|
+
}>;
|
|
9
|
+
export interface AuthHandlerArgs {
|
|
10
|
+
basedir: string;
|
|
11
|
+
forceArg: boolean;
|
|
12
|
+
provider: string;
|
|
13
|
+
authDecoderImport?: string;
|
|
14
|
+
webAuthn?: boolean;
|
|
15
|
+
webPackages?: string[];
|
|
16
|
+
apiPackages?: string[];
|
|
17
|
+
extraTasks?: (ListrTask<AuthGeneratorCtx> | undefined)[];
|
|
18
|
+
notes?: string[];
|
|
19
|
+
verbose?: boolean;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* basedir assumes that you must have a templates folder in that directory.
|
|
23
|
+
* See folder structure of auth providers in packages/auth-providers/<provider>/setup/src
|
|
24
|
+
*/
|
|
25
|
+
export declare const standardAuthHandler: ({ basedir, forceArg, provider, authDecoderImport, webAuthn, webPackages, apiPackages, extraTasks, notes, verbose, }: AuthHandlerArgs) => Promise<void>;
|
|
26
|
+
//# sourceMappingURL=setupHelpers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"setupHelpers.d.ts","sourceRoot":"","sources":["../../src/auth/setupHelpers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAA;AAGvC,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,OAAO,CAAA;AAWjC,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAA;AAUtD,eAAO,MAAM,mBAAmB,UAAW,IAAI;;;;EAoB9C,CAAA;AAED,MAAM,WAAW,eAAe;IAC9B,OAAO,EAAE,MAAM,CAAA;IACf,QAAQ,EAAE,OAAO,CAAA;IACjB,QAAQ,EAAE,MAAM,CAAA;IAChB,iBAAiB,CAAC,EAAE,MAAM,CAAA;IAC1B,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,WAAW,CAAC,EAAE,MAAM,EAAE,CAAA;IACtB,WAAW,CAAC,EAAE,MAAM,EAAE,CAAA;IACtB,UAAU,CAAC,EAAE,CAAC,SAAS,CAAC,gBAAgB,CAAC,GAAG,SAAS,CAAC,EAAE,CAAA;IACxD,KAAK,CAAC,EAAE,MAAM,EAAE,CAAA;IAChB,OAAO,CAAC,EAAE,OAAO,CAAA;CAClB;AASD;;;GAGG;AACH,eAAO,MAAM,mBAAmB,wHAW7B,eAAe,kBAiFjB,CAAA"}
|