forked from nsjcy/frontEnd/nsjcy

liuwh
2020-04-27 4e44bc1fd7806a6c1611302120882b91d96640b5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const chalk = require('chalk');
const path = require('path');
const fs = require('fs');
 
 
function srcpath(basedir) {
  const srcdir = path.join(basedir, 'src');
  if (fs.existsSync(srcdir)) {
    return {
      basedir,
      srcdir
    };
  }
  const { base, dir } = path.parse(basedir);
  if (!base) {
    console.log(chalk.red('src directory not found.'));
    process.exit();
  }
  return srcpath(dir);
}
 
const {
  srcdir, basedir
} = srcpath(process.env.INIT_CWD);
 
 
const entries = fs.readdirSync(srcdir)
  .map(file => path.join(srcdir, file))
  .filter(file => {
    if (fs.statSync(file).isDirectory()) {
      return false;
    }
    const ext = path.extname(file);
    return ext && '.js.jsx.ts.tsx'.includes(ext);
  });
 
const entry = {};
const htmls = [];
const includes = process.argv.slice(2);
for (const entryfile of entries) {
  const {
    ext, name
  } = path.parse(entryfile);
  if (includes.length && !includes.includes(name)) {
    continue;
  }
  const html = path.join(srcdir, 'conf', `${name}.html`);
  if (fs.existsSync(html)) {
    console.log(chalk.green(`Page ${name}:`));
    console.log(chalk.green(`    ${html}`));
    htmls.push({ name, html });
    entry[name] = [
      path.join(srcdir, 'conf', `${name}.reset.scss`),
      path.join(srcdir, 'conf', `reset.scss`),
      entryfile,
      path.join(srcdir, 'conf', `${name}.cover.scss`),
      path.join(srcdir, 'conf', `cover.scss`)
    ].filter(file => {
      if (fs.existsSync(file)) {
        console.log(chalk.green(`    ${file}`));
        return true;
      } else {
        console.log(chalk.yellow(`    ${file}`));
        return false;
      }
    });
 
  } else {
    console.log(chalk.red(`Page ${name}:`));
    console.log(chalk.red(`    ${html}`));
  }
}
console.log('\n');
if (htmls.length) {
  module.exports = {
    basedir,
    entry,
    htmls
  };
} else {
  console.log(chalk.red('Entry file not found...'));
  process.exit();
}