13af6ab5fSopenharmony_ci/*
23af6ab5fSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
33af6ab5fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
43af6ab5fSopenharmony_ci * you may not use this file except in compliance with the License.
53af6ab5fSopenharmony_ci * You may obtain a copy of the License at
63af6ab5fSopenharmony_ci *
73af6ab5fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
83af6ab5fSopenharmony_ci *
93af6ab5fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
103af6ab5fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
113af6ab5fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123af6ab5fSopenharmony_ci * See the License for the specific language governing permissions and
133af6ab5fSopenharmony_ci * limitations under the License.
143af6ab5fSopenharmony_ci */
153af6ab5fSopenharmony_ci
163af6ab5fSopenharmony_ciimport type {Node, TransformerFactory} from 'typescript';
173af6ab5fSopenharmony_ciimport {lstatSync, readdirSync} from 'fs';
183af6ab5fSopenharmony_ciimport {join, resolve} from 'path';
193af6ab5fSopenharmony_ci
203af6ab5fSopenharmony_ciimport type {IOptions} from '../configs/IOptions';
213af6ab5fSopenharmony_ciimport type {TransformPlugin} from './TransformPlugin';
223af6ab5fSopenharmony_ciimport { Extension } from '../common/type';
233af6ab5fSopenharmony_ci
243af6ab5fSopenharmony_ciexport class TransformerManager {
253af6ab5fSopenharmony_ci  private static readonly sLoadPath: string = join(__dirname, '../', 'transformers');
263af6ab5fSopenharmony_ci
273af6ab5fSopenharmony_ci  private readonly mTransformers: TransformerFactory<Node>[];
283af6ab5fSopenharmony_ci
293af6ab5fSopenharmony_ci  public constructor(options: IOptions) {
303af6ab5fSopenharmony_ci    this.mTransformers = [];
313af6ab5fSopenharmony_ci    this.loadTransformers(options);
323af6ab5fSopenharmony_ci  }
333af6ab5fSopenharmony_ci
343af6ab5fSopenharmony_ci  public getTransformers(): TransformerFactory<Node>[] {
353af6ab5fSopenharmony_ci    return this.mTransformers;
363af6ab5fSopenharmony_ci  }
373af6ab5fSopenharmony_ci
383af6ab5fSopenharmony_ci  private loadTransformers(options: IOptions): void {
393af6ab5fSopenharmony_ci    let subFiles: string[] = readdirSync(TransformerManager.sLoadPath);
403af6ab5fSopenharmony_ci    let plugins: TransformPlugin[] = [];
413af6ab5fSopenharmony_ci    for (const subFile of subFiles) {
423af6ab5fSopenharmony_ci      let subPath: string = resolve(TransformerManager.sLoadPath + '/' + subFile);
433af6ab5fSopenharmony_ci      let isDir: boolean = lstatSync(subPath)?.isDirectory();
443af6ab5fSopenharmony_ci      if (!isDir) {
453af6ab5fSopenharmony_ci        continue;
463af6ab5fSopenharmony_ci      }
473af6ab5fSopenharmony_ci
483af6ab5fSopenharmony_ci      let subDir: string[] = readdirSync(subPath);
493af6ab5fSopenharmony_ci      for (const file of subDir) {
503af6ab5fSopenharmony_ci        if (!file.endsWith(Extension.TS)) {
513af6ab5fSopenharmony_ci          continue;
523af6ab5fSopenharmony_ci        }
533af6ab5fSopenharmony_ci        const fileNameNoSuffix = file.lastIndexOf(Extension.DTS) > -1 ? file.slice(0, file.lastIndexOf(Extension.DTS)) :
543af6ab5fSopenharmony_ci          file.slice(0, file.lastIndexOf(Extension.TS));
553af6ab5fSopenharmony_ci        let path: string = join(subPath, fileNameNoSuffix);
563af6ab5fSopenharmony_ci        let module = require(path);
573af6ab5fSopenharmony_ci        let plugin: TransformPlugin = module?.transformerPlugin;
583af6ab5fSopenharmony_ci        if (plugin) {
593af6ab5fSopenharmony_ci          plugins.push(plugin as TransformPlugin);
603af6ab5fSopenharmony_ci        }
613af6ab5fSopenharmony_ci      }
623af6ab5fSopenharmony_ci    }
633af6ab5fSopenharmony_ci
643af6ab5fSopenharmony_ci    plugins.sort((plugin1: TransformPlugin, plugin2: TransformPlugin) => {
653af6ab5fSopenharmony_ci      return plugin1.order - plugin2.order;
663af6ab5fSopenharmony_ci    });
673af6ab5fSopenharmony_ci
683af6ab5fSopenharmony_ci    plugins.forEach((plugin: TransformPlugin) => {
693af6ab5fSopenharmony_ci      let transformerFactory: TransformerFactory<Node> = plugin?.createTransformerFactory(options);
703af6ab5fSopenharmony_ci      let name: string = plugin?.name;
713af6ab5fSopenharmony_ci      if (transformerFactory && name) {
723af6ab5fSopenharmony_ci        this.mTransformers.push(transformerFactory);
733af6ab5fSopenharmony_ci      }
743af6ab5fSopenharmony_ci    });
753af6ab5fSopenharmony_ci  }
763af6ab5fSopenharmony_ci}
77