@ivoryio/ivory-cli
Version:
40 lines (39 loc) • 1.96 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.injectAuthCode = void 0;
var path_1 = require("path");
var shelljs_1 = require("shelljs");
var importLocation = '/* IMPORT-LOCATION */';
exports.injectAuthCode = function () {
injectAuthProvider();
injectAuthRoutes();
injectAliasConfig();
};
function injectAuthProvider() {
var rootComponent = path_1.join(process.cwd(), 'src', 'app', 'Root.tsx');
var startLocation = '{/* PROVIDERS-START */}';
var endLocation = '{/* PROVIDERS-END */}';
var importStatement = "import { AuthProvider } from '@auth'";
var tagOpen = '<AuthProvider>\n';
var tagClose = '</AuthProvider>\n';
shelljs_1.sed('-i', escape(importLocation), importLocation + "\n" + importStatement, rootComponent);
shelljs_1.sed('-i', escape(startLocation), tagOpen + " " + startLocation, rootComponent);
shelljs_1.sed('-i', escape(endLocation), tagClose + " " + endLocation, rootComponent);
}
function injectAuthRoutes() {
var routerComponent = path_1.join(process.cwd(), 'src', 'app', 'Router.tsx');
var importStatement = "import { AuthenticationScreen, ProtectedRoute } from '@auth'";
var routeLocation = "<DashboardScreen path='/dashboard' />";
var authRoutes = "<ProtectedRoute component={DashboardScreen} path='/dashboard' />\n <AuthenticationScreen path='/auth' />";
shelljs_1.sed('-i', escape(importLocation), importLocation + "\n" + importStatement, routerComponent);
shelljs_1.sed('-i', escape(routeLocation), "" + authRoutes, routerComponent);
}
function injectAliasConfig() {
var tsconfig = path_1.join(process.cwd(), 'tsconfig.alias.json');
var aliasLocation = "\"paths\": {";
var authAlias = " \"@auth\": [ \"./modules/@auth\" ],";
shelljs_1.sed('-i', escape(aliasLocation), aliasLocation + "\n" + authAlias, tsconfig);
}
function escape(str) {
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}