1
1
/* Imports */
2
2
let markdownpdf = require ( process . cwd ( ) + '/markdown-pdf/index' )
3
+ // Markdown PDF is bundled separately because there are issues running phantom with render.js when
4
+ // bundled as an executable with nexe
5
+
3
6
let fileSystem = require ( 'fs' )
4
7
let path = require ( 'path' )
5
8
6
9
7
10
/* Removes Original Markdown Files and Folders from directory */
8
11
let removeDir = ( path ) => {
9
12
if ( fileSystem . existsSync ( path ) ) {
10
- /*Delete Markdown Folders with assets */
13
+ /* Ensure that software files and generated PDFs are not deleted */
11
14
let files = fileSystem . readdirSync ( path ) . filter ( ( file ) => {
12
15
return file != 'node_modules'
13
16
&& file != 'markdown-pdf'
@@ -23,8 +26,8 @@ let path = require('path')
23
26
24
27
}
25
28
)
26
- console . log ( files )
27
29
30
+ /* Recursive Deletion of Markdowns and their folders */
28
31
if ( files . length > 0 ) {
29
32
files . forEach ( function ( filename ) {
30
33
if ( fileSystem . statSync ( path + "/" + filename ) . isDirectory ( ) ) {
@@ -58,20 +61,26 @@ let path = require('path')
58
61
59
62
/* Create an Array of Markdown Files to be Converted */
60
63
let markdownFiles = [ ]
61
- console . log ( 'Directory being checked is ' )
62
- console . log ( __dirname )
63
64
fileSystem . readdirSync ( process . cwd ( ) ) . forEach ( file => {
64
- console . log ( file )
65
65
if ( file . includes ( '.md' ) ) {
66
66
markdownFiles . push ( file )
67
67
}
68
68
} )
69
+ if ( markdownFiles . length === 0 ) {
70
+ /* Only exit when user presses a key */
71
+ console . log ( '\nPlease add some markdown files to the same folder as the exe file' )
72
+ console . log ( 'Press any key to exit' ) ;
73
+
74
+ process . stdin . setRawMode ( true ) ;
75
+ process . stdin . resume ( ) ;
76
+ process . stdin . on ( 'data' , process . exit . bind ( process , 0 ) ) ;
77
+ }
69
78
console . log ( 'Files to be converted :' )
70
79
console . log ( markdownFiles )
71
80
72
81
/* Create a book with all markdown files */
73
82
let bookPath = process . cwd ( ) + "/pdfs/book.pdf"
74
- console . log ( 'Creating a book...' )
83
+ console . log ( '\n\nCreating a book...' )
75
84
markdownpdf ( { phantomPath : path . join ( process . cwd ( ) , 'phantomjs.exe' ) } ) . concat . from ( markdownFiles ) . to ( bookPath , function ( ) {
76
85
console . log ( "Created a book at" , bookPath )
77
86
@@ -88,10 +97,11 @@ markdownpdf({phantomPath: path.join(process.cwd(), 'phantomjs.exe')}).concat.fro
88
97
pdfDocs . forEach ( function ( d ) { console . log ( "Created" , d ) } )
89
98
90
99
/*Delete Original Markdown Files and Folders */
91
- console . log ( 'Deleting Original Markdown Files...' )
100
+ console . log ( '\n\nDeleting Original Markdown Files...' )
92
101
removeDir ( process . cwd ( ) )
93
102
94
103
/* Only exit when user presses a key */
104
+ console . log ( '\nCompleted, PDFs can be found in the pdfs folder :)' )
95
105
console . log ( 'Press any key to exit' ) ;
96
106
97
107
process . stdin . setRawMode ( true ) ;
0 commit comments