#!/usr/bin/env node
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const chalk_1 = __importDefault(require("chalk"));
const console_1 = require("console");
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const prompts_1 = __importDefault(require("prompts"));
const customtypes_1 = require("./customtypes");
(async () => {
    const banner = chalk_1.default `
                _|_|                                        _|    _|                
    _|_|_|    _|        _|_|_|    _|_|_|                _|_|_|  _|_|_|_|    _|_|_|  
  _|_|      _|_|_|_|  _|        _|        _|_|_|_|_|  _|    _|    _|      _|_|      
      _|_|    _|      _|        _|                    _|    _|    _|          _|_|  
  _|_|_|      _|        _|_|_|    _|_|_|                _|_|_|      _|_|  _|_|_|    
                                                                                    
                                                                    `;
    console_1.log(chalk_1.default.hex('ed26f3')(banner));
    console_1.log(`Welcome to ${chalk_1.default.magentaBright('sfcc-dts')} custom attributes definition generator.\n`);
    let defaultpath = './sites/site_template/meta/';
    let extensionspath;
    if (fs_1.default.existsSync(defaultpath)) {
        console_1.log(`directory ${defaultpath} available`);
        extensionspath = defaultpath;
    }
    else {
        const response = await prompts_1.default([
            {
                type: 'text',
                name: 'meta',
                message: 'Directory containing system-objecttype-extensions.xml?',
                initial: defaultpath,
                validate: value => !fs_1.default.existsSync(value) ? `directory ${value} not found` : true
            }
        ]);
        extensionspath = response.meta;
    }
    console_1.log(`Generating definitions for custom attributes`);
    if (extensionspath) {
        await customtypes_1.generateCustomTypes(extensionspath);
    }
    console_1.log(`Write @types/dw/index.d.ts`);
    let references = '/// <reference path="../../node_modules/sfcc-dts/@types/sfcc/index.d.ts" />\n';
    if (extensionspath) {
        references += '/// <reference path="./attrs.d.ts" />\n';
    }
    else {
        references += '/// <reference path="../../node_modules/sfcc-dts/@types/sfcc/attrs.d.ts" />\n';
    }
    fs_1.default.writeFileSync(path_1.default.join('@types/dw', 'index.d.ts'), references);
    console_1.log(`\nDone!`);
})();
